locked
Regex match for 3 numbers

    Question

  • 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

     

    Wednesday, June 13, 2007 1:54 PM

Answers

  • 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.

    Input TextThis is a test 100 9999 22
    Regular Expression

    (?(\b\d+\b)            # If it is a number
      (?(\b\d{1,3}\b)      # Then If it is 1-3 digits
        (?<Number>\b\d+\b) # Capture to the Number named group
       |
        (?<TooBig>[^\s]*)  # Too big of a number. 
       )
     |                     # Or from the first if
      (?<Word>[^\s.!?]*)   # It is a word
    )
    (?:\s?|$|[.?!])        # Capture but don't match the nonwords.

    Group CapturesGroups: (0) (Number) (TooBig) (Word)


    Match (1):
                  0 : This 
             Number : 
             TooBig : 
               Word : This

    Match (2):
                  0 : is 
             Number : 
             TooBig : 
               Word : is

    Match (3):
                  0 : a 
             Number : 
             TooBig : 
               Word : a

    Match (4):
                  0 : test 
             Number : 
             TooBig : 
               Word : test

    Match (5):
                  0 : 100 
             Number : 100
             TooBig : 
               Word : 

    Match (6):
                  0 : 9999 
             Number : 
             TooBig : 9999
               Word : 

    Match (7):
                  0 : 22
             Number : 22
             TooBig : 
               Word : 

    Match (8):
                  0 : 
             Number : 
             TooBig : 
               Word : 

    Regex Options
                                                                                                
    CompileECMASingle LineMulti LineIgnore White SpaceExplicit CaptureIgnore CaseRight To LeftCulture Invariant
            

    Regex Info Generated by the Regex Responder V1.0

    Wednesday, June 13, 2007 8:03 PM

All replies

  • 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 2:11 PM
  • How about:

     

    Code Snippet

    "\D?(\d{3})\D?"

     

    Wednesday, June 13, 2007 3:51 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 4:31 PM
  •  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:12 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) Then

          Return word

       End If

    Next

     

    Thanks for any ideas,

    Tom

     

    Wednesday, June 13, 2007 7:44 PM
  • 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.

    Input TextThis is a test 100 9999 22
    Regular Expression

    (?(\b\d+\b)            # If it is a number
      (?(\b\d{1,3}\b)      # Then If it is 1-3 digits
        (?<Number>\b\d+\b) # Capture to the Number named group
       |
        (?<TooBig>[^\s]*)  # Too big of a number. 
       )
     |                     # Or from the first if
      (?<Word>[^\s.!?]*)   # It is a word
    )
    (?:\s?|$|[.?!])        # Capture but don't match the nonwords.

    Group CapturesGroups: (0) (Number) (TooBig) (Word)


    Match (1):
                  0 : This 
             Number : 
             TooBig : 
               Word : This

    Match (2):
                  0 : is 
             Number : 
             TooBig : 
               Word : is

    Match (3):
                  0 : a 
             Number : 
             TooBig : 
               Word : a

    Match (4):
                  0 : test 
             Number : 
             TooBig : 
               Word : test

    Match (5):
                  0 : 100 
             Number : 100
             TooBig : 
               Word : 

    Match (6):
                  0 : 9999 
             Number : 
             TooBig : 9999
               Word : 

    Match (7):
                  0 : 22
             Number : 22
             TooBig : 
               Word : 

    Match (8):
                  0 : 
             Number : 
             TooBig : 
               Word : 

    Regex Options
                                                                                                
    CompileECMASingle LineMulti LineIgnore White SpaceExplicit CaptureIgnore CaseRight To LeftCulture Invariant
            

    Regex Info Generated by the Regex Responder V1.0

    Wednesday, June 13, 2007 8:03 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 1:57 PM
  •  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.
    Thursday, June 14, 2007 2:28 PM
  • 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 10:55 AM
  • Hi 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...
    Saturday, June 16, 2007 5:44 PM
  • Very helpful. Thanks for the link!
    Sunday, June 17, 2007 1:15 AM