Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i obtain pattern string from compiled regexp pattern in python

Tags:

python

regex

I have some code like this one:

>>> import re >>> p = re.compile('my pattern') >>> print p _sre.SRE_Pattern object at 0x02274380 

Is it possible to get string "my pattern" from p variable?

like image 897
Mykola Kharechko Avatar asked Sep 12 '09 19:09

Mykola Kharechko


People also ask

What is the best way to parse strings and find patterns in Python?

regex = r"([a-zA-Z]+) (\d+)" if re.search(regex, "Jan 2"): match = re.search(regex, "Jan 2") # This will print [0, 5), since it matches at the beginning and end of the # string print("Match at index %s, %s" % (match. start(), match. end())) # The groups contain the matched values. In particular: # match.

How do you search for a regex pattern at the beginning of a string in Python?

match() function of re in Python will search the regular expression pattern and return the first occurrence. The Python RegEx Match method checks for a match only at the beginning of the string. So, if a match is found in the first line, it returns the match object.


1 Answers

p.pattern 

Read more about re module here: http://docs.python.org/library/re.html

like image 63
Mikhail Churbanov Avatar answered Oct 23 '22 04:10

Mikhail Churbanov