Regular Expression Syntax Help

E

Ernesto

I'm trying to get the right syntax for my regular expression. The
string I'm trying to parse is:

# myString
[USELESS DATA]
Name: David Dude
[USELESS DATA]

Right now, I'm using the following code:


pattern_Name= '''(?x)
Title:\s+(\w+)\s+
'''
names = re.findall(pattern_Name, myString)
print names

This prints out a list containing only the first names. I want to
search the string until it finds a '\n' endline, but I experimented
with that, and couldn't find what I need.
 
E

Ernesto

The word "Title" there should be "Name".

What I really need is the pattern for getting the entire string after
"Name: " until a '\n' is found.
 
R

Raja Raman Sundararajan

try this. maybe this is what you want?

reg = re.compile('Name:.*\\n', re.IGNORECASE)
 
E

Ernesto

So now I need to add the requirement that only "Name:" 's which are
followed by

"Request: Play" or "Request: Next" ANYWHERE between the previous titles
and the new titles. Can I use RE's for that ?
 
R

Raja Raman Sundararajan

Oh! yes you can use re for that.
You just need to change the pattern a bit

I did not understand where the "title" will be so I have ignored it,
but I got something below which will be helpful for you
value = """name:asasasasas\nrequest: play\ntitle"""
reg = re.compile('Name:.*\\nrequest:.....', re.IGNORECASE)
re.findall(reg, value) ['name:asasasasas\nrequest: play']
value2 = """name:asasasasas\nrequest: next\ntitle"""
reg = re.compile('Name:.*\\nrequest:.....', re.IGNORECASE)
re.findall(reg, value2) ['name:asasasasas\nrequest: next']
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top