6

I am new to regex, why does this not output 'present'?

tale = "It was the best of times, ... far like the present ... of comparison only. "
a = re.compile('p(resent)')
print a.findall(tale)

>>>>['resent']
6
  • coz the group your regex is searching is 'resent' here. can you provide a better example of expected input/output? Commented Dec 25, 2012 at 2:55
  • 1
    I expected it to output the string 'present' in a list of one element ('present' only occurs once in the string, tale). I believed that I was searching for instances of 'resent' preceded by a 'p'. Commented Dec 25, 2012 at 2:58
  • expected output for this : tale="resent present ppresent presentt"? Commented Dec 25, 2012 at 3:04
  • @Chet, the groups are what's inside of the parentesis Commented Dec 25, 2012 at 3:05
  • The output I expected from the above line was: ['resent','present'] Commented Dec 25, 2012 at 3:14

2 Answers 2

3

try something like this if you're trying to match the exact word present here:

In [297]: tale="resent present ppresent presentt"

In [298]: re.findall(r"\bpresent\b",tale)
Out[298]: ['present']
Sign up to request clarification or add additional context in comments.

Comments

1

From the Python documentation

If one or more groups are present in the pattern, return a list of groups

If you want it to just use the group for grouping, but not for capturing, use a non-capture group:

a = re.compile('p(?:resent)')

For this regular expression, there's no point in it, but for more complex regular expressions it can be appropriate, e.g.:

a = re.compile('p(?:resent|eople)')

will match either 'present' or 'people'.

6 Comments

Ok, cool. This explains how to do what I was trying to do. Can you explain to me what my previous code was looking for (why was the p disregarded)? Thank you very much.
It was being used in matching, but not being captured in the return info. An example would be if you wanted to find a percentage, but only cared about the number part -- you would use the regex (\d+)%.
So once parentheses are included, only things inside the parenthesis are captured?
@Chet, when using re.findall, yes. If no parentheses are found then the entire string will be the result element. When using search or match, you can specify the entire string by using result.group(0) or access the groups with result.group(1) etc.
@Barmar I'm not too familiar with Python regex, but could OP have used two groups in his/her regex? Say if a = re.compile('(p(resent))')?
|

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.