Re: strings patterns and the like

From: burchill (burchill_at_btinternet.com)
Date: 12/06/04


Date: Mon, 06 Dec 2004 00:56:59 +0000

Ryan Stewart wrote:
> "Nicholas Parnell" <053267p@acadiau.ca> wrote in message
> news:cp06hs$253e$1@poseidon.acadiau.ca...
> *fixed top post*
>
>>"burchill" <burchill@btinternet.com> wrote in message
>>news:31hju6F3ae192U1@individual.net...
>>
>>>I have been playing with the string tokenizer and the string split
>>>method and have had some success. However neither of them do exactly
>>>what I want.
>>>
>>>What I need is to be able to search a string and extract any sequence of
>>> 6 characters that contain any digits from 1 to 9 and/or the character
>>>x.
>>>
>>>For example...
>>>
>>>xx0232
>>>355433
>>>x02220
>>>
>>>What is the best way to do this ?, I have looked at patterns and regular
>>>expressions but I can't see anything that fits.
>>>
>>>Any help apprecaited.
>>>
>>>--
>>>Eps
>>
>>You could take the string and have a loop to look at each character in the
>>string. Then you could have an array of size 6 which checks the current
>>position to see if it's an x or a digit... if it is, store it in the first
>>element in the array... if the next character is an x or a digit, put it
>>in
>>the 2nd element of the array and so on. If you fill up the array (counter
>>=
>>6), then you have your sequence... if you reach a character that is not a
>>digit or an x, then the counter starts back to zero and you start filling
>>the array at position 0 again.
>>
>>Hope this helps!
>>
>
> Or you could do it the easy way and use regular expressions. "[\\dx]{6}"
> should match what you want. "\d" in a regex means any digit. To get a single
> backslash in the regex, you have to double it in the String since the
> backslash is a Java escape character. The "x" of course just means x. The []
> brackets mean "anything in here". The "{6}" means exactly six times. So
> you'll get: (any digit or x) ocurring six times in a row. There are a few
> different ways to use regexes in code. See the docs for the java.util.regex
> package. Here's a simple example:
>
> import java.util.regex.*;
> public class TestMain {
> public static void main(String[] args) {
> String matchMe = "45x557123456asdfh444888dsxx9988";
> Pattern p = Pattern.compile("([\\dx]{6})");
> Matcher m = p.matcher(matchMe);
> int searchPos = 0;
> while (searchPos < matchMe.length() && m.find(searchPos)) {
> System.out.println("Found " + m.group() + " at " + m.start());
> searchPos = m.start() + 6;
> }
> }
> }

Thank you very much, I thought regular expressions would probably be
able to do it but the java api docs can be difficult to follow sometimes.

Thanks for your help.

--
Eps


Relevant Pages

  • Help in Spanish translation of the description of UDFs
    ... functions of minimum / maximum values among elements of an array column. ... GETALLWORDS- Inserts the words from a string into a global dimensioned ... WORDTRAN- Searches a character string for occurrences of a first word, ... ARRAYSUM- Returns the sum of all or a specified range of numeric (and/or ...
    (microsoft.public.fox.helpwanted)
  • Re: Check for Common character sequence ( I will pay)?
    ... Do I need to return an array? ... You need to identify character sequences of 3 or more characters that appear ... in more than one string. ... and test each 3-character sequence that results. ...
    (microsoft.public.dotnet.framework)
  • Re: Desirable Usage of Fortran Modules
    ... suppose we want to cast a character array as ... A function that returns a string ... array of double precision numbers: ...
    (comp.lang.fortran)
  • Re: Check for Common character sequence ( I will pay)?
    ... Yes you are returning an array of FoundString objects. ... in more than one string. ... This means that you have to identify sequences 1 character at a time, ... Again, obviously, if the 3-character sequence doesn't match, neither will ...
    (microsoft.public.dotnet.framework)
  • Re: Mod 43 Check Digit calculator
    ... miscalculation of the check digit. ... > response to you) should return the check character itself rather than> the ... Here is that formula with the absolute reference problem> that ... > space in the encoding string at the 39th position. ...
    (microsoft.public.excel.programming)