1

I have a list of strings in python and if an element of the list contains the word "parthipan" I should print a message. But the below script is not working

import re
a = ["paul Parthipan","paul","sdds","sdsdd"]
last_name = "Parthipan"
my_regex = r"(?mis){0}".format(re.escape(last_name))
if my_regex in a:
    print "matched"

The first element of the list contains the word "parthipan", so it should print the message.

3 Answers 3

2

If you want to do this with a regexp, you can't use the in operator. Use re.search() instead. But it works with strings, not a whole list.

for elt in a:
    if re.search(my_regexp, elt):
        print "Matched"
        break # stop looking

Or in more functional style:

if any(re.search(my_regexp, elt) for elt in a)):
    print "Matched"
Sign up to request clarification or add additional context in comments.

Comments

2

You don't need regex for this simply use any.

>>> a = ["paul Parthipan","paul","sdds","sdsdd"]
>>> last_name = "Parthipan".lower()
>>> if any(last_name in name.lower() for name in a):
...     print("Matched")
... 
Matched

Comments

1

Why not:

a = ["paul Parthipan","paul","sdds","sdsdd"]
last_name = "Parthipan"
if any(last_name in ai for ai in a):
    print "matched"

Also what for is this part:

...
import re
my_regex = r"(?mis){0}".format(re.escape(last_name))
...

EDIT:

Im just too blind to see what for do You need regex here. It would be best if You would give some real input and output. This is small example which could be done in that way too:

a = ["paul Parthipan","paul","sdds","sdsdd",'Mala_Koala','Czarna,Pala']
last_name = "Parthipan"
names=[]
breakers=[' ','_',',']
for ai in a:
    for b in breakers:
        if b in ai:
            names.append(ai.split(b))
full_names=[ai for ai in names if len(ai)==2]
last_names=[ai[1] for ai in full_names]

if any(last_name in ai for ai in last_names):
    print "matched"

But if regex part is really needed I cant imagine how to find '(?mis)Parthipan' in 'Parthipan'. Most simple would be in reverse direction 'Parthipan' in '(?mis)Parthipan'. Like here...

import re
a = ["paul Parthipan","paul","sdds","sdsdd",'Mala_Koala','Czarna,Pala']
last_name = "Parthipan"
names=[]
breakers=[' ','_',',']
for ai in a:
    for b in breakers:
        if b in ai:
            names.append(ai.split(b))
full_names=[ai for ai in names if len(ai)==2]
last_names=[r"(?mis){0}".format(re.escape(ai[1])) for ai in full_names]
print last_names
if any(last_name in ai for ai in last_names):
    print "matched"

EDIT:

Yhm, with regex You have few possibilities...

import re
a = ["paul Parthipan","paul","sdds","sdsdd",'jony-Parthipan','koala_Parthipan','Parthipan']
lastName = "Parthipan"
myRegex = r"(?mis){0}".format(re.escape(lastName))

strA=';'.join(a)
se = re.search(myRegex, strA)
ma = re.match(myRegex, strA)
fa = re.findall(myRegex, strA)
fi=[i.group() for i in re.finditer(myRegex, strA, flags=0)]
se = '' if se is None else se.group()
ma = '' if ma is None else ma.group()

print se, 'match' if any(se) else 'no match'
print ma, 'match' if any(ma) else 'no match'
print fa, 'match' if any(fa) else 'no match'
print fi, 'match' if any(fi) else 'no match'

output, only first one seems ok, so only re.search gives proper solution:

Parthipan match
 no match
['Parthipan', 'Parthipan', 'Parthipan', 'Parthipan'] match
['Parthipan', 'Parthipan', 'Parthipan', 'Parthipan'] match

3 Comments

No need to import re in your first code snippet as you don't use it any more...
The part you don't understand requires regexp!
@Robert, you're off track. Read the documentation to find out what (?...) does.

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.