user469652
user469652

Reputation: 51211

python regular expression help

Need to match any string ends up with a letter, and the second last character is '>'

It will match:

abc>a
ddd_4>f

It will not match:

abc>ab
abc>2

Upvotes: 1

Views: 101

Answers (4)

Jiří Polcar
Jiří Polcar

Reputation: 1222

>>> import re
>>> s = 'abc>a'
>>> r = re.compile(r'>[:alpha:]$')
>>> print( r.search(s) )
<_sre.SRE_Match object at 0xb76c5a30>
>>> 

If you want to match letters according to locales.

Upvotes: 3

Tom
Tom

Reputation: 1

I think this is what you are looking for:

import re 
re.search(">[a-zA-Z]$", str)

It will evaluate to None if the string does not match.

Upvotes: 0

moinudin
moinudin

Reputation: 138317

.*>[a-zA-Z]$

>>> for s in ('abc>a', 'ddd_4>f', 'abc>ab', 'abc>2'):
...     print re.match(r'.*>[a-zA-Z]$', s)
... 
<_sre.SRE_Match object at 0xb7217e58>
<_sre.SRE_Match object at 0xb7217e58>
None
None

Upvotes: 0

Justin Peel
Justin Peel

Reputation: 47072

re.compile(r'.*>[a-zA-Z]$')

should generate the pattern that you want.

However, I recommend that you take a read through the regexp part of Google's Python class. Then you can learn how to do things like this yourself.

Upvotes: 1

Related Questions