0

What would be the python regex for the following string?

"111(A5)A05209(May)2005"

I want to get the values:

   111
   A5
   A05209
   May
   2005

Thanks!

2
  • 2
    You know... you should post your attempt as well. See this. Commented Jul 9, 2013 at 19:45
  • One possible (and trivial) regex for that string would be 111\(A5\)A05209\(May\)2005. I think what you're looking for is more likely a way to split the string on a set of delimiters (i.e. re.split())... Commented Jul 9, 2013 at 19:49

4 Answers 4

5

Simply use re.split. Probably the most intuitive method.

>>> import re
>>> re.split(r'[\(\)]', "111(A5)A05209(May)2005")
['111', 'A5', 'A05209', 'May', '2005']
Sign up to request clarification or add additional context in comments.

Comments

2

The easiest

s = " 111(A5)A05209(May)2005"
s.replace('(', ' ').replace(')', ' ')
values = s.split()
>> ['111', 'A5', 'A05209', 'May', '2005']

The regexp way would be

import re
s = re.findall(r'\w+', s)
>> ['111', 'A5', 'A05209', 'May', '2005']

Comments

1

Use re.findall and str.join:

>>> import re
>>> strs = "111(A5)A05209(May)2005"
>>> print "\n".join(re.findall(r'\w+',strs))
111
A5
A05209
May
2005

or re.sub:

>>> print re.sub(r'[\W]+','\n',strs)
111
A5
A05209
May
2005

Another alternative is str.translate:

>>> from string import punctuation, whitespace, maketrans
>>> intab = punctuation + whitespace
>>> outtab = "\n"*len(intab)
>>> print strs.translate(trantab)
111
A5
A05209
May
2005

In terms of performance str.translate is far better than regex:

>>> strs = "111(A5)A05209(May)2005"*1000
>>> %timeit "\n".join(re.findall(r'\w+',strs))
100 loops, best of 3: 2.19 ms per loop
>>> %timeit re.sub(r'[\W]+','\n',strs)
100 loops, best of 3: 4.43 ms per loop
>>> %timeit strs.translate(trantab)
10000 loops, best of 3: 93.9 us per loop

1 Comment

Dammit! You thwart me again! :)
0
>>> str = '111(A5)A05209(May)2005'
>>> print str.replace('(','\n').replace(')','\n')
111
A5
A05209
May
2005

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.