Regex match for 3 numbers
-
Wednesday, June 13, 2007 1:54 PM
Hello,
I am trying to find a 3 digit numeric string in a sentence. The regex expression I am using is "\d{3}". This works for strings like "100", but it also matches strings like "9999". I would just like a match for "999" but not "9999".
What regex should I use in this case?
Thanks
Tom
All Replies
-
Wednesday, June 13, 2007 2:11 PM
the answer depends on the text u r processing and how the processing is implelentedd. pls post details.
for one: do u process on one-string-at-a-time basis OR u r processing the whole chunk of multiline text? It depends..
that's why people on forums are always advised not to save on keyboard clicks and privide more details...
-
Wednesday, June 13, 2007 3:51 PM
How about:
Code Snippet"\D?(\d{3})\D?"
-
Wednesday, June 13, 2007 4:31 PM
\D/\d{3}\D?
still will match on bolded part of this input:
12389
unless we know details of text processing [processing a string or the whole text/sentence? ], we can't really know how to match \d{3} in the target text.
-
Wednesday, June 13, 2007 7:12 PMModerator
Mister T554781 wrote: The regex expression I am using is "\d{3}". This works for strings like "100", but it also matches strings like "9999". I would just like a match for "999" but not "9999".
I agree with Sergei...more info is needed but in answering the basic question about eliminating a match so it only matches three numerics, use \b which signifies word boudaies such as
\b\d{1,3}\b
which will look for words of digits, 1-3 in length, such as
1
12
123
but not
1234 -
Wednesday, June 13, 2007 7:44 PM
Thanks to everyone for their reply.
I am taking a sentence and splitting it into a string array. Then looping thru the array to find the match.
Dim myRegex As New Regex("\d{3}", RegexOptions.IgnoreCase)Dim words() as String = "This is a test 100 9999 22".Split(" ")
Dim word as string = ""
For Each word in words
If
myRegex.IsMatch(word) ThenReturn word
End IfNext
Thanks for any ideas,
Tom
-
Wednesday, June 13, 2007 8:03 PMModerator
Why not let regex do the parsing? The following regex will extract the words and number into named capture groups of Number, TooBig and Word. All you have to do is check the current match for a Number and it is guareenteed to be 1-3 in size. Note match index 0 is the whole match. You will want to index via the named capture group not the index number.
Regex Info Generated by the Regex Responder V1.0
-
Thursday, June 14, 2007 1:57 PM
OmegaMan,
Thanks for your answer!
I will checkout your Regex Responder too. It looks like a real time saver.
Thanks
Tom
-
Thursday, June 14, 2007 2:28 PMModerator
Mister T554781 wrote: OmegaMan,
Thanks for your answer!
I will checkout your Regex Responder too. It looks like a real time saver.
Thanks
Tom
The responder is only for responding to HTML forum posts and not for general regex uses. I recommend you use Ultrapico's (Expresso 3.0) to work out the kinks with regex patterns before using them in code. -
Saturday, June 16, 2007 10:55 AM
Hello OmegaMan,
Can you please elaborate a bit on your solution? Is (?) some kind of a conditional pattern? I have not seen this before. I appreciate your contribution to this forum.
-
Saturday, June 16, 2007 5:44 PMModeratorHi J2,
I have done just that on my blog entitled, Regular Expressions and the If Conditional which discusses this solution. Let me know if it helps... -
Sunday, June 17, 2007 1:15 AMVery helpful. Thanks for the link!

