TechTalkz.com Logo Ask the Expert

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > Java

Notices

Reg expression matching issue

Java


Reply
 
Thread Tools Display Modes
Old 07-12-2007, 05:14 PM   #21
Tim Smith
Guest
 
Posts: n/a
Re: Reg expression matching issue

On 2007-12-06, Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote:
>> but it shouldn't match the following:
>>
>> asdjkas hdjasdhsajkd
>> 7674367346
>> 33
>> asdjkas 343

....
> 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());
> }
> }


Nope. That will match strings that contain runs of 5 or more digits.
He wants runs of exactly 5 digits.
  Reply With Quote
Old 07-12-2007, 05:14 PM   #22
Tim Smith
Guest
 
Posts: n/a
Re: Reg expression matching issue

On 2007-12-06, Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote:
>> but it shouldn't match the following:
>>
>> asdjkas hdjasdhsajkd
>> 7674367346
>> 33
>> asdjkas 343

....
> 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());
> }
> }


Nope. That will match strings that contain runs of 5 or more digits.
He wants runs of exactly 5 digits.
  Reply With Quote
Old 07-12-2007, 05:14 PM   #23
Tim Smith
Guest
 
Posts: n/a
Re: Reg expression matching issue

On 2007-12-06, Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote:
> 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.
  Reply With Quote
Old 07-12-2007, 05:14 PM   #24
Tim Smith
Guest
 
Posts: n/a
Re: Reg expression matching issue

On 2007-12-06, Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote:
> 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.
  Reply With Quote
Old 07-12-2007, 05:21 PM   #25
Tim Smith
Guest
 
Posts: n/a
Re: Reg expression matching issue

On 2007-12-06, Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote:
>> but it shouldn't match the following:
>>
>> asdjkas hdjasdhsajkd
>> 7674367346
>> 33
>> asdjkas 343

....
> 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());
> }
> }


Nope. That will match strings that contain runs of 5 or more digits.
He wants runs of exactly 5 digits.
  Reply With Quote
Old 07-12-2007, 05:21 PM   #26
Tim Smith
Guest
 
Posts: n/a
Re: Reg expression matching issue

On 2007-12-06, Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote:
> 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.
  Reply With Quote
Old 07-12-2007, 05:28 PM   #27
Tim Smith
Guest
 
Posts: n/a
Re: Reg expression matching issue

On 2007-12-06, Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote:
>> but it shouldn't match the following:
>>
>> asdjkas hdjasdhsajkd
>> 7674367346
>> 33
>> asdjkas 343

....
> 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());
> }
> }


Nope. That will match strings that contain runs of 5 or more digits.
He wants runs of exactly 5 digits.
  Reply With Quote
Old 07-12-2007, 05:29 PM   #28
Tim Smith
Guest
 
Posts: n/a
Re: Reg expression matching issue

On 2007-12-06, Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote:
> 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.
  Reply With Quote
Old 07-12-2007, 05:42 PM   #29
Tim Smith
Guest
 
Posts: n/a
Re: Reg expression matching issue

On 2007-12-06, Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote:
>> but it shouldn't match the following:
>>
>> asdjkas hdjasdhsajkd
>> 7674367346
>> 33
>> asdjkas 343

....
> 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());
> }
> }


Nope. That will match strings that contain runs of 5 or more digits.
He wants runs of exactly 5 digits.
  Reply With Quote
Old 07-12-2007, 05:43 PM   #30
Tim Smith
Guest
 
Posts: n/a
Re: Reg expression matching issue

On 2007-12-06, Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote:
> 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.
  Reply With Quote
Reply

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 09:36 PM.


vBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2010, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional