0

Suppose I have a string:

string = 'AvBvC'

I want to match A, B, and C, and this is what I did:

match = re.search('(.*)v(.*)', string)
print match.groups()

The problem is, the result shows that:

('AvB', 'C',)

instead of what I want, which is

('A', 'B', 'C',)

How do I make it catch all overlapping patterns..?

Thanks.

(I know there are some posts concerning the same issue, but haven't found a definite answer for Python)

8
  • Please spell out the exact output you want. Can't guess. Commented Dec 8, 2013 at 4:01
  • @Tim Peters sorry I want ('A', 'B', 'C',) Commented Dec 8, 2013 at 4:01
  • @PeterDeGlopper Yeah!! just modified my post (my original code is way longer and more complicated than this,, sorry) Commented Dec 8, 2013 at 4:04
  • 1
    More details would help - you can do what the above question asks with just split('v'), so I can only assume you have a more complicated situation. Commented Dec 8, 2013 at 4:06
  • This may be what you are after? stackoverflow.com/questions/5616822/… Commented Dec 8, 2013 at 4:06

2 Answers 2

2

Use re.split

>>> import re
>>> re.split('v', 'AvBvC')
['A', 'B', 'C']

And to demonstrate further...

>>> re.split('vw', 'AAvwBBvwCC')
['AA', 'BB', 'CC']
Sign up to request clarification or add additional context in comments.

Comments

2

Your question is somewhat unclear, you seem to have more of a complicated string than you actual show.

Using search() matches only the first occurrence, you can use findall() to match all occurrences.

matches = re.findall(r'[^v]+', string)
['A', 'B', 'C']

Another option would be to split on certain characters that you need to split on.

>>> re.split('v', 'AvBvC')
['A', 'B', 'C']

10 Comments

This works for my example, but my actual string contains things that are not single character such as A,B,C... So is there a way to do this while using "search" instead of "findall"??
search() and match() return exactly 1 result. So, no. You need to make your question clearer ;-)
@TimPeters Why negative lookahead doesnt allow me to match beginning of the string?
@thefourtheye, huh? Without some context (or code), I don't know what you're asking - sorry.
@thefourtheye, then some people are bound to get confused by sloppy terminology ;-) The "fixed width" restriction is unique to look-behinds - it does not apply to look-aheads. The reason is simply the sheer difficulty of implementing varying-width look-behinds; they're "highly unnatural" for a "left to right" search engine.
|

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.