Reputation: 5119
I am trying to match the following types of strings:
123456
1234.56
123,456
1,234.56
.123456
I want to make sure that these strings aren't matched:
12,34.56
1.23,456
This is what I've come up with so far (with help from a previous post on this forum):
(?<acreage>((\d{1,3}(,\d{3})+)?|\d*)\.\d+|(\d{1,3}(,\d{3})+|\d+)\.?)
This is matching all the valid strings, but it is also matching parts of the invalid strings.
What I'd like is to put some kind of boundary condition at the start, but it can't be a ^ character because there might be text before the acreage I'm trying to match. The \b character treats commas as a word boundary and so for a string like the following,
12,34,567.89
It will return 34,567.89 when I really want it to fail to match any part of the string.
I will be checking for specific characters after the acreage string, so I'm not so worried about it doing goofy stuff like getting 123,456 & 7 from 123,4567.
Could someone please help me to get this to only match the valid strings and not match any of the invalid strings?
Upvotes: 1
Views: 743
Reputation: 93046
Is this enough?
(?<![\d.,])(?<acreage>((\d{1,3}(,\d{3})+)?|\d*)\.\d+|(\d{1,3}(,\d{3})+|\d+)\.?)(?![\d.,])
I added a negative lookbehind (?<![\d.,])
and a negative lookahead (?![\d.,])
assertion to your expression.
(?<![\d.,])
ensures that there is no digit, no .
and no ,
before
(?![\d.,])
ensures that there is no digit, no .
and no ,
following
See it here on Regexr
That would be your personal number boundary, that checks that around your number are characters that are not allowed in the number.
Upvotes: 1