![]() |
![]() |
|
|||||||
| Register | Forum Rules | Getting Started! - Guide | Blog | Videos | Gallery | Members List | Social Groups | Mark Forums Read |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
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. |
|
|
|
#22 |
|
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. |
|
|
|
#23 |
|
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. |
|
|
|
#24 |
|
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. |
|
|
|
#25 |
|
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. |
|
|
|
#26 |
|
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. |
|
|
|
#27 |
|
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. |
|
|
|
#28 |
|
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. |
|
|
|
#29 |
|
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. |
|
|
|
#30 |
|
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. |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
< Home - Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |