Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Regex Capture Only Certain Text [duplicate]

Tags:

python

regex

I am trying to find functionality in python similar to the Ruby function scan. My goal is to grab all the text in-between two curly braces in a list. If there are multiple pairs of curly braces in the string, I want to have multiple entries in the list.

When I run this code:

 match = re.search(r'\{(.+)\}', request.params['upsell'])
 print match.group()

I match the right text. However, what is captured includes the curly braces. I don't want to include this text, I want to include everything in between, but not the curly braces Thanks!

like image 531
Spencer Avatar asked Sep 20 '11 20:09

Spencer


People also ask

How do you capture a regular expression in Python?

To capture all matches to a regex group we need to use the finditer() method. The finditer() method finds all matches and returns an iterator yielding match objects matching the regex pattern. Next, we can iterate each Match object and extract its value.

Does re match return a string?

Both re.search() and re. match() returns only the first occurrence of a substring in the string and ignore others.

How do you match only a string in Python?

The $ means "match the end of the string". If you want to match alphanumerics, you'd need something like ^[0-9a-zA-Z]+$ if you want to use regexes. If you want to build these regexes piece by piece, you'd want to use an alternation like [0-9]|[a-zA-Z] and then wrap it in ^(...)+$


1 Answers

Use group(1), or lookbehinds/aheads. (Also, be sure to take the advice of F.J. and J.F. and use either .+? or [^{}]*

import re
match = re.search(r'\{(.+)\}', "asdfasd {asdf}asdfasdf")
print match.group(1)

or with lookbehinds/aheads:

import re
match = re.search(r'(?<=\{)(.+)(?=\})', "asdfasd {asdf}asdfasdf")
print match.group()
like image 142
Jacob Eggers Avatar answered Sep 19 '22 00:09

Jacob Eggers