Re: strings patterns and the like
From: burchill (burchill_at_btinternet.com)
Date: 12/06/04
- Next message: Martman: "Re: java for the .Net developer hence jsp vs servlet"
- Previous message: Ryan Stewart: "Re: strings patterns and the like"
- In reply to: Ryan Stewart: "Re: strings patterns and the like"
- Next in thread: Nicholas Parnell: "Re: strings patterns and the like"
- Reply: Nicholas Parnell: "Re: strings patterns and the like"
- Reply: burchill: "Re: strings patterns and the like"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Martman: "Re: java for the .Net developer hence jsp vs servlet"
- Previous message: Ryan Stewart: "Re: strings patterns and the like"
- In reply to: Ryan Stewart: "Re: strings patterns and the like"
- Next in thread: Nicholas Parnell: "Re: strings patterns and the like"
- Reply: Nicholas Parnell: "Re: strings patterns and the like"
- Reply: burchill: "Re: strings patterns and the like"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|