• Post Reply Bookmark Topic Watch Topic
  • New Topic

RegEx to match the first five-digit number  RSS feed

 
Mike London
Ranch Hand
Posts: 1476
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to write a match expression that will only match the first five-digit number in a string.

For example: \b\d{5}\b

will match both five-digit numbers in the string: text 12345 123 49322 2343045983

I've experimented making sure "Greedy" is off and trying to use a back reference with a capturing group, but nothing so far has worked.

The output should just be the first 12345 five-digit number.

Not sure how to do this and don't see any obvious examples online that work.

TIA

- mike
 
Tim Moores
Saloon Keeper
Posts: 3900
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're only interested in the first group, why don't you just stop processing after handling that match?
 
Mike London
Ranch Hand
Posts: 1476
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:If you're only interested in the first group, why don't you just stop processing after handling that match?


Yes, well, that is my question ... "how" to do just that?

- mike
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike London wrote:
Yes, well, that is my question ... "how" to do just that?


Break out of the loop? Or don't use a loop? ... regardless, perhaps you should show us some code? as this is more about the regex api, than the regex expression.

Henry
 
Mike London
Ranch Hand
Posts: 1476
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Mike London wrote:
Yes, well, that is my question ... "how" to do just that?


Break out of the loop? Or don't use a loop? ... regardless, perhaps you should show us some code? as this is more about the regex api, than the regex expression.

Henry


My bad. Sorry for my confusing reply above.

What I was asking was whether there's a way to have a RegEx that does the single match by itself. That is, just the RegEx expression independent of Java code.

Java would fix all problems and I could figure that part out. I was just asking a higher-level question.
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So... you need to match a 5 digit number that is not preceded by another 5 digit number.

If you really want to do this with just a single regular expression, you need a zero-width negative lookbehind.
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, you can't do this because lookbehinds need to be fixed length, and you need a quantifier in your lookbehind to match the characters between your 5 digit numbers.

The best way to do this is using just the Matcher.find() method.
 
Tobias Bachert
Ranch Hand
Posts: 85
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lookbehind will actually work if finite lookbehinds are supported by the regex engine.
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah that won't work because your lookbehind isn't actually fixed width.
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Actually, you can't do this because lookbehinds need to be fixed length, and you need a quantifier in your lookbehind to match the characters between your 5 digit numbers.


Interestingly, it just needs to be bounded... so, instead of ".*" for the characters in between, it has to be something like ".{,1000}",  or some other very large number, so that the total match is less than maxint. And yes, it is kinda a hack...

Regardless, I like this...have a cow.

Henry
 
Tobias Bachert
Ranch Hand
Posts: 85
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java9 supports it, for Java8 Henrys alternative works.
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool, are you on an early release?

And thanks for the cow Henry
 
Mike London
Ranch Hand
Posts: 1476
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ended up just getting the first match in Java rather than having the RegEx itself be able to extract just the first five-digit number.

Thanks to all.

- mike
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike London wrote:I ended up just getting the first match in Java rather than having the RegEx itself be able to extract just the first five-digit number.


Yea, that is probably the better option, as the code is probably much more readable than changing your regex to  ...
"(?<!\\b\\d{5}\\b.{,1000})\\b\\d{5}\\b"
  ... as discussed in this topic ...

Henry
 
Mike London
Ranch Hand
Posts: 1476
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Mike London wrote:I ended up just getting the first match in Java rather than having the RegEx itself be able to extract just the first five-digit number.


Yea, that is probably the better option, as the code is probably much more readable than changing your regex to  ...
"(?<!\\b\\d{5}\\b.{,1000})\\b\\d{5}\\b"
  ... as discussed in this topic ...

Henry


Yep, that RegEx would be understood and remembered for about 4 hours. ;)

Thanks again.

- mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!