Home > Software engineering >  Regex get string after specific char, but only when the text starts with a specific string
Regex get string after specific char, but only when the text starts with a specific string

Time:04-01

I have a list of values that contains various values, but I'm only interested in the number after # of those starting with XXX_

ABC

XXX_YYY

XXX_YYY#12235

XXX_YYY#12281

XXX_YYY#12318

I have tried several things but not quite hit the head of the nail :-(

(?<!XXX\_)#

and

(?<=XXX\_)\*\[^#\] $ - closest but also get those without # in :-(

CodePudding user response:

To get the number after #, please find below python code and modify as per need

import re    
result = re.findall("(?<=#)(.*?)(?=$)",a)
print(result[0])

CodePudding user response:

Both patterns do not take numbers into account, and will match:

  • (?<!XXX_)# only matches a single # when not directly preceded by XXX_

  • (?<=XXX_)*[^#] $ Optionally repeats a lookbehind assertion, and then matches 1 chars other than # till the end of the string.


If there is a single # char in the string before the numbers, you can match XXX_ followed by any char except # using a negated character class and then match # followed by capturing the digits at the end of the string in group 1.

XXX_[^\n#]*#(\d )$

The pattern matches:

  • XXX_ Match literally
  • [^\n#]*# Match optional chars other than # or a newline, then match #
  • (\d ) Capture 1 digits in group 1
  • $ End of string

See a regex demo.

  • Related