Tuesday, May 3, 2011

Regex with unusual characters

I'm trying to come up with some regex to handle the # sign.

Example, #PRODUCT_143#

If the input were #PRODUCT_143, the regex #PRODUCT_(\d*$) matches and returns 143 as the match. But adding the # to the end of both the input and the regex causes it to break. what do I need to do here to get this to match?

From stackoverflow
  • If you tried #PRODUCT_(\d*$)# it’s no surprise that it didn’t found a match. Since the $ already marks the end of the string and the # after it will never be matched.

    So try this instead:

    #PRODUCT_(\d*)#$
    
  • It shouldn't break anything, maybe you've forgotten to move the $ in the regex, which means end of line? This regex should be fine:

    #PRODUCT_(\d*)#$
    

    (I'm not sure why you were capturing the end of line in your original regex).

  • Did you just forget to move the $ out of the parenthesis?

        ^#PRODUCT_([0-9]*)#$
    

    Note the added ^ - may be it is not right for your input.

  • The $ probably matches as the end-of-line, so your extra # is confusing it:

    #PRODUCT_(\d*)#$
    

0 comments:

Post a Comment