WongSifu
WongSifu

Reputation: 547

python regular expression with "OR" and $ end of line

I have 3 different types of string in which I'm trying to match a pattern: Patterns:

  1. abcdedf_abc_abc_XS123456789_1234567
  2. abcdedf_abc_abc_AB_1234567_2014/03/17
  3. abcdef_abcdf_abc_xyz_12354AB12_1234567

In each case I'm tying to match '1234567'

The code that I have tried is:

m = re.search(r'_[0-9]+_|$',string)
m = re.search(r'_[0-9]+[_]|$',string)

None seem to be giving me the results that I want.

From http://docs.python.org/2/howto/regex.html I have read that:

"Alternation, or the “or” operator. If A and B are regular expressions, A|B will match any string that matches either A or B. | has very low precedence in order to make it work reasonably when you’re alternating multi-character strings. Crow|Servo will match either Crow or Servo, not Cro, a 'w' or an 'S', and ervo."

A and B must be regular expressions. I'm guessing that $ is not a regular expression.

Upvotes: 0

Views: 746

Answers (1)

thefourtheye
thefourtheye

Reputation: 239653

strings = [
"abcdedf_abc_abc_XS123456789_1234567",
"abcdedf_abc_abc_AB_1234567_2014/03/17",
"abcdef_abcdf_abc_xyz_12354AB12_1234567"
]

import re
pattern = re.compile(r"(?<=_)\d+(?=_|$)")
for item in strings:
    print pattern.search(item).group()

Output

1234567
1234567
1234567

Regular expression visualization

Debuggex Demo

Upvotes: 3

Related Questions