0

I'm new to python and am trying to figure out python regex to find any strings that match -. For example, 'type1-001' and 'type2-001' should be a match, but 'type3-asdf001' shouldn't be a match. I would like to be able to match with a regex like [type1|type2|type3]-\d+ to find any strings that start with type1, type2, or type3 and then are appended with '-' and digits. Also, it would be cool to know how to search for any upper case text appended with '-' and digits.

Here's what I think should work, but I can't seem to get it right...

pref_num = re.compile(r'[type1|type2]-\d+')
3
  • 1
    Just FYI here, but a regex that searches for multiple strings is kind of redundant, as the whole point of a regex is to search for multiple strings. If you were only searching for one string, you could test for membership with in, and the match would simply be the string itself. Commented May 20, 2015 at 17:43
  • @TigerhawkT3 Not really. I can give you regexes that search for a single static string but includes multiple conditions that are not possible using a simple text search. Commented May 20, 2015 at 17:47
  • @poke nailed it, my example is simplified, but i need to search for single static strings that include multiple conditions that aren't possible with simple text search (without keeping/updating a list of all of those strings) Commented May 20, 2015 at 18:11

4 Answers 4

3

[] will match any of the set of characters appearing between the brackets. To group regexes you need to use (). So, I think your regex should be something like:

pref_num = re.compile(r'(type1|type2)-\d+')

As to how to search any uppercase text appended with - and digits, I would suggest:

[A-Z]+-\d+
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this answers both of my questions. Perfect. Also, the () vs [] was key to matching the group regex.
2

If you only want the digit after "type" to be variable then you should put only those in the square brackets like so:

re.compile(r'type[1|2]-\d+')

Comments

0

You can use the pattern

'type[1-3]-[0-9]{3}'

Demo

>>> import re
>>> p = 'type[1-3]-[0-9]{3}'
>>> s = 'type2-005 with some text type1-101 and then type1-asdf001'
>>> re.findall(p, s)
['type2-005', 'type1-101']

Comments

0
pref_num = re.compile(r'(type1|type2|type3)-\d+')

m = pref_num.search('type1-000')
if m != None: print(m.string)

m = pref_num.search('type2-000')
if m != None: print(m.string)

m = pref_num.search('type3-abc000')
if m != None: print(m.string)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.