0

Hi trying to separate each of the expressions from the colon. So want to obtain 'ss_vv', 'kk' and 'pp'. But the two print expressions below give me 'v' and 'k', so getting parts only of each string. Can anyone see what wrong here?

m0 = re.compile(r'([a-z]|_)+:([a-z]|_)+:([a-z]|_)+')
m1 = m0.search('ss_vv:kk:pp')
print m1.group(1)
print m1.group(2)
1
  • A simple split on : ? Commented Aug 26, 2016 at 5:49

3 Answers 3

4
In [52]: m0 = re.compile(r'([a-z|_]+):([a-z|_]+):([a-z|_]+)')

In [53]: m1 = m0.search('ss_vv:kk:pp')

In [54]: print m1.group(1)
ss_vv

In [55]: print m1.group(2)
kk

In [56]: print m1.group(3)
pp

What my regex does:

([a-z|_]+):([a-z|_]+):([a-z|_]+)

Regular expression visualization

Debuggex Demo

What your regex does:

([a-z]|_)+:([a-z]|_)+:([a-z]|_)+

Regular expression visualization

Debuggex Demo

Sign up to request clarification or add additional context in comments.

Comments

1

No need to use regex for your case. You can just split on basis of ':' and get required output..

>>> a = 'ss_vv:kk:pp'
>>> b_list = a.split(':')
>>> b_list
['ss_vv', 'kk', 'pp']
>>>

Comments

1

What are the other rules for the regular expression? based on your question, this regex would do:

m0 = re.compile(r'(.*):(.*):(.*)')
m1 = m0.search('ss_vv:kk:pp')
print m1.group(1)
print m1.group(2)

UPDATE: as mentioned by @Jan in the comments, for efficient and better use of regex, you can modify it as

regex = r'([^:]+):([^:]+):([^:]+)'
m0 = re.compile(regex)

output:

ss_vv
kk

or by just splitting the string:

string = 'ss_vv:kk:pp'
parts = string.split(':')

print parts

outputs: ['ss_vv', 'kk', 'pp']

3 Comments

If I am not mistaken, I believe [^:]* would not work?
Not on ist own, of course. What I meant, is the difference between [^:]*:[^:]*:[^:\n]* and (.*):(.*):(.*) - do you see the significant reduction in steps? The former needs 36, while the latter needs 284 (9 times!). My point is: the dot-star (.*) brings you down the line and then* backtracks* - which can be very ineffective sometimes. As a general rule of thumb I would say, try to be more specific and try to come to an end faster with regular expressions. Nevertheless, +1 for the splitting option here :)
oh... yes, I see your point.. that's very informative.. :) thanks!

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.