0

I'm working with regular expressions in Python and I'm struggling with this. I have data in a file of lines like this one:

|person=[[Old McDonald]]

and I just want to be able to extract Old McDonald from this line.

I have been trying with this regular expression:

matchLine = re.match(r"\|[a-z]+=(\[\[)?[A-Z][a-z]*(\]\])", line)
print matchLine

but it doesn't work; None is the result each time.

2
  • extract means you are to change line ? Commented Feb 28, 2013 at 9:07
  • I just want to be able to store Old McDonald from this line, so I can put it in a dictionary. Commented Feb 28, 2013 at 9:10

1 Answer 1

3

Construct [A-Z][a-z]* does not match Old McDonald. You probably should use something like [A-Z][A-Za-z ]*. Here is code example:

import re
line = '|person=[[Old McDonald]]'
matchLine = re.match ('\|[a-z]+=(?:\[\[)?([A-Z][A-Za-z ]*)\]\]', line)
print matchLine.group (1)

The output is Old McDonald for me. If you need to search in the middle of the string, use re.search instead of re.match:

import re
line = 'blahblahblah|person=[[Old McDonald]]blahblahblah'
matchLine = re.search ('\|[a-z]+=(?:\[\[)?([A-Z][A-Za-z ]*)\]\]', line)
print matchLine.group (1)
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, I don't think it's working for me. I get this error: AttributeError: 'NoneType' object has no attribute 'group'
@user2057841 Are you running my example exactly as it is or you copied my regex to your code?
Ok, I got it!! Thanks so much.
Note that re.match searches in the beginning of the string only. If you want to search for possible occurences in the middle of the string, use re.search instead.

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.