Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

This regex should be simple but I'm stumped

Last post 03-08-2010, 11:01 PM by Aussie Susan. 3 replies.
Sort Posts: Previous Next
  •  03-07-2010, 9:00 PM 60428

    This regex should be simple but I'm stumped

    In string "2601-2650 21 - - - - - - - - - - -21 15 - - - - - - - - - ", I want to capture the 2nd occurrence of "-2". I came up with "[^\d]-\d" but it'll return " -2" (preceded by a space) not "-2". I'm using PCRE engine.

     

    TIA

     

       
      

  •  03-08-2010, 7:30 PM 60500 in reply to 60428

    Re: This regex should be simple but I'm stumped

    The regex is actually returning exactly what you have asked it for: a hyphen followed by a digit that is not preceded by another digit. A space is not a digit and therefore you get the match you want.

    Are you specifically wanting the second occurrence of "-2" or of "a hyphen followed by a digit? If the first, then try:

    -2.*?(-2)

    and what you want will be captured in match group #1. If the latter then:

    -\d.*?(-\d)

    Note that your "rules" for the match talk about the "2nd occurrence" and nothing about what comes immediately before the "-2" (even if this is generalised to "-digit"). Therefore your pattern needs to reflect the "2nd occurrence" and not "what comes before".

    If I've misunderstood your requirements,  then please tell us what they are a biut more clearly.

    Susan

  •  03-08-2010, 8:45 PM 60502 in reply to 60500

    Re: This regex should be simple but I'm stumped

    Hi Susan,

        Thank you so much for taking the time to reply. I apologize for the error in my first post, there were typos when I state the result that I wanted vs the result that I got.

    You're correct, what I want is a "hyphen followed by a digit". But only the second occurrence of it. For example, in the following strings:

    2601-2650 21 - - - - - - - - - - -21 15 - - - - - - - - -
    3801-3850 146 54 48 42 37 31 25 19 13 - - -146 136 93 87 82 76 70 64 58 52 47
    3851-3900 152 58 52 46 40 34 28 23 17 11 - -152 142 132 91 85 79 73 68 62 56 50
    3901-3950 158 128 55 49 44 38 32 26 20 14 - -158 148 138 128 89 83 77 71 65 59 54
    3951-4000 164 134 59 53 47 41 35 30 24 18 12 -164 154 144 134 92 86 80 75 69 63 57
    4001-4050 170 140 130 56 51 45 39 33 27 21 16 10 170 160 150 140 130 90 84 78 72 66 61

    The result that I'm hoping to see for each line respectively would be;

    -2

    -1

    -1

    -1

    -1

    < none >

    I'm still a rookie with regex and any help you could provide is appreciated. Thank you

     

     



  •  03-08-2010, 11:01 PM 60514 in reply to 60502

    Re: This regex should be simple but I'm stumped

    The second pattern - '-\d.*?(-\d)' - will do that and you need to look at the text captured in match group #1.

    By the way, is there any need for the digits to be the same? In other words would "12-356-678" match on the "-6"? If so then you would need:

    -(\d).*?(-\1)

    In this case you would use the characters in match group #2.

    Susan

View as RSS news feed in XML