Cemre Mengü
Cemre Mengü

Reputation: 18754

Python regex matching only if digit

Given the regex and the word below I want to match the part after the - (which can also be a _ or space) only if the part after the delimiter is a digit and nothing comes after it (I basically want to to be a number and number only). I am using group statements but it just doesn't seem to work right. It keeps matching the 3 at the beginning (or the 1 at the end if I modify it a bit). How do I achieve this (by using grouping) ?

Target word: BR0227-3G1

Regex: ([A-Z]*\s?[0-9]*)[\s_-]*([1-9][1-9]*)

It should not match 3G1, G1 , 1G

It should match only pure numbers like 3,10, 2 etc.

Here is also a helper web site for evaluating the regex: http://www.pythonregex.com/

More examples:

It should match:

BR0227-3
BR0227 3
BR0227_3

into groups (BR0227) (3)

It should only match (BR0227) for

BR0227-3G1
BR0227-CS
BR0227
BR0227-

Upvotes: 0

Views: 7250

Answers (3)

Ian Clelland
Ian Clelland

Reputation: 44122

Since you want the start and (possible) end of the word in groups, then do this:

r'\b([A-Z0-9]+)(?:[ _-](\d+))?\b'

This will put the first part of the word in the first group, and optionally the remainder in the second group. The second group will be None if it didn't match.

Upvotes: 1

Hoopdady
Hoopdady

Reputation: 2356

This should match anything followed by '-', ' ', or '_' with only digits after it.

(.*)[- _](\d+)

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191729

I would use

re.findall('^([A-Z]*\s?[0-9]*)[\s_-]*([1-9][1-9]*$)?', str)

Each string starts with the first group and ends with the last group, so the ^ and $ groups can assist in capture. The $ at the end requires all numbers to be captured, but it's optional so the first group can still be captured.

Upvotes: 2

Related Questions