Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert SRE_Match object to string

Tags:

python

regex

The output of my re.search returns <_sre.SRE_Match object at 0x10d6ed4e0> I was wondering how could I convert this to a string? or a more readable form?

like image 334
Liondancer Avatar asked Apr 12 '14 02:04

Liondancer


People also ask

Does re match return a string?

Both return the first match of a substring found in the string, but re. match() searches only from the beginning of the string and return match object if found. But if a match of substring is found somewhere in the middle of the string, it returns none.

What is an RE match object?

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.

How do you get the actual strings that match the pattern from a match object?

How do you get the actual strings that match the pattern from a Match object? The group() method returns strings of the matched text.


2 Answers

You should do it as:

result = re.search(your_stuff_here) if result:     print result.group(0) 
like image 109
sshashank124 Avatar answered Sep 24 '22 23:09

sshashank124


If you want to see all groups in order:

result = re.search(your_stuff_here) if result:     print result.groups() 
like image 29
JOM Avatar answered Sep 26 '22 23:09

JOM