> Hi,
> I am trying to find a regular expression for 5 consequent numbers like
[quoted text clipped - 20 lines]
> Thanks,
> Ross
import java.util.regex.*;
public class test2 {
public static void main(String[] args) {
String str = ";alsdlkajsdf 32344 lkls3 3323 llsdfsdf";
Pattern p = Pattern.compile(".*\\d{5}.*");
Matcher m = p.matcher(str);
System.out.println(m.matches());
}
}

Signature
Knute Johnson
email s/nospam/knute/
Tim Smith - 07 Dec 2007 08:19 GMT
>> but it shouldn't match the following:
>>
>> asdjkas hdjasdhsajkd
>> 7674367346
>> 33
>> asdjkas 343
...
> import java.util.regex.*;
>
[quoted text clipped - 7 lines]
> }
> }
Nope. That will match strings that contain runs of 5 or more digits.
He wants runs of exactly 5 digits.
Stefan Ram - 07 Dec 2007 17:28 GMT
>Nope. That will match strings that contain runs of 5 or more digits.
>He wants runs of exactly 5 digits.
Wouldn't it be smart if the OP had posted a compilable
test program with his own implementation which still
fails the test?
Then anyone who believes he has a solution could use the
test program of the OP to test his solution before replying.
Tim Smith - 07 Dec 2007 08:35 GMT
> Pattern p = Pattern.compile(".*\\d{5}.*");
> Matcher m = p.matcher(str);
Here's a way to fix that so it won't incorrectly match "foo123456bar"
and similar strings:
Pattern p = Pattern.compile(".*[^0-9]\\d{5}[^0-9].*");
Matcher m = p.matcher("a"+str+"a");
The "a"+str+"a" ensures that we do not have to worry about the special
cases of the 5 digits being the first or last digits of the string, and
the change to the regular expression ensures that the 5 digits are a run
of exactly 5 digits, by requiring that they be bordered by a non-digit.
Lew - 07 Dec 2007 15:07 GMT
>> Pattern p = Pattern.compile(".*\\d{5}.*");
>> Matcher m = p.matcher(str);
[quoted text clipped - 9 lines]
> the change to the regular expression ensures that the 5 digits are a run
> of exactly 5 digits, by requiring that they be bordered by a non-digit.
Or they could use find() instead of matches() and avoid all that String
manipulation overhead.

Signature
Lew
Stefan Ram - 07 Dec 2007 17:31 GMT
>Pattern p = Pattern.compile(".*[^0-9]\\d{5}[^0-9].*");
Will this match the text "34344" (from the OP)
with just five digits and nothing else?
Lasse Reichstein Nielsen - 07 Dec 2007 17:54 GMT
>>Pattern p = Pattern.compile(".*[^0-9]\\d{5}[^0-9].*");
>
> Will this match the text "34344" (from the OP)
> with just five digits and nothing else?
No, which is why the text is modified before the pattern is applied.
But a more complex pattern could do it directly:
"(^|.*[^\\d])\\d{5}([^\\d].*|$)"
/L

Signature
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Stefan Ram - 07 Dec 2007 18:05 GMT
>No, which is why the text is modified before the pattern is applied.
Sorry! I had not read your previous post carefully enough.
Lasse Reichstein Nielsen - 07 Dec 2007 19:43 GMT
>>No, which is why the text is modified before the pattern is applied.
>
> Sorry! I had not read your previous post carefully enough.
Oh, it wasn't mine. I was just about to make a similar response when
I did read the rest :)
/L

Signature
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Tim Smith - 08 Dec 2007 01:31 GMT
> But a more complex pattern could do it directly:
> "(^|.*[^\\d])\\d{5}([^\\d].*|$)"
I tried something like that, but must have got it wrong. (99.9% of my
regular expression work is done in Perl, and so I probably botched
something translating to Java).
It would be interesting to see if it is faster to have the more complex
pattern, or use the simple pattern and add the guards to the string.
Joshua Cranmer - 08 Dec 2007 14:43 GMT
> But a more complex pattern could do it directly:
> "(^|.*[^\\d])\\d{5}([^\\d].*|$)"
Equivalent but simpler:
"(^|.*\\D)\\d{5}(\\D.*|$)"
Said regex could be made to match only the zipcode by making the first
set of parentheses a negative-lookbehind and the second one a
negative-lookahead.
The regex I would prefer:
"\\b\\d{5}\\b" (does not match `a12345' though)

Signature
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
Faraz.ya@gmail.com schreef:
> Hi,
> I am trying to find a regular expression for 5 consequent numbers like
[quoted text clipped - 18 lines]
>
> Can somebody help me please
String.matches() looks whether the entire string matches the given
pattern. So you have to account for the characters before and after the
five digits as well, as Knute pointed out. Another way would be to use
java.util.regex.Matcher.find(), which finds the next match of a given
regex in a string. This is probably what you want.
H.

Signature
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
bugbear - 06 Dec 2007 10:38 GMT
> Faraz.ya@gmail.com schreef:
>> Hi,
[quoted text clipped - 25 lines]
> java.util.regex.Matcher.find(), which finds the next match of a given
> regex in a string. This is probably what you want.
I suspect it's more efficient too. In general
I'd expect smaller regexps to be cheaper
BugBear
Knute Johnson - 06 Dec 2007 17:45 GMT
> Faraz.ya@gmail.com schreef:
>> Hi,
[quoted text clipped - 27 lines]
>
> H.
What Hendrik said and Matcher.find() is also handy if you want to find
multiple matching subsequences.
import java.util.regex.*;
public class test2 {
public static void main(String[] args) {
String str = ";alsdlkajsdf 32344 lkls3 3323 llsdfsdf 12345";
Pattern p = Pattern.compile("\\d{5}");
Matcher m = p.matcher(str);
while (m.find())
System.out.println(m.group());
}
}

Signature
Knute Johnson
email s/nospam/knute/