0

I have a string of type "animal cat dog" and I am trying to extract animal from it.

Following this example, I tried using re.search (and also re.match later), however that didn't produce the result I expected. The if statement would go through but groups() would be empty.

The code I had:

string = "fox cat dog"
regex = "\S+ cat dog\s*"
m = re.search(regex, string)    
if m:
    temp = m.group(1)

I tried printing out m and m.groups() and they had the following values:

m: <_sre.SRE_Match object at 0x000000002009A920>  
m.groups(): ()

I found a way around the problem by using substrings and .find() but I am very curious what was wrong with my original code.

Any help would be appreciated. Thank you!

Community
  • 1
  • 1
Nyan Octo Cat
  • 159
  • 2
  • 4
  • 8

1 Answers1

4

You just need to add a parenthesis to the group you want. Like so:

string = "fox cat dog"
regex = "(\S+) cat dog\s*"
#       ~~~~~~Note the parenthesis there
m = re.search(regex, string)    
if m:
    temp = m.group(1)

You may want to check the documentation for more information:

(...) Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group; the contents of a group can be retrieved after a match has been performed, and can be matched later in the string with the \number special sequence, described below. To match the literals '(' or ')', use \( or \), or enclose them inside a character class: [(] [)].

azalea
  • 11,402
  • 3
  • 35
  • 46