regex function driving me nuts

M

MartinD.

Hi,

I'm new to Python.
Does someone has an idea what's wrong. I tried everything. The only regex that is tested is the last one in a whole list of regex in keywords.txt
Thanks!
Martin


########
def checkKeywords( str, lstKeywords ):

for regex in lstKeywords:
match = re.search(regex, str,re.IGNORECASE)
# If-statement after search() tests if it succeeded
if match:
print match.group() ##just debugging
return match.group() ## 'found!

return

#########

keywords1 = [line for line in open('keywords1.txt')]
resultKeywords1 = checkKeywords("string_to_test",keywords1)
print resultKeywords1
 
P

Prasad, Ramit

MartinD said:
Hi,

I'm new to Python.
Does someone has an idea what's wrong. I tried everything. The only regex that is tested is the last one in a
whole list of regex in keywords.txt
Thanks!
Martin


########
defcheckKeywords( str, lstKeywords ):

for regex in lstKeywords:
match = re.search(regex, str,re.IGNORECASE)
# If-statement after search() tests if it succeeded
if match:
print match.group() ##just debugging
return match.group() ## 'found!

return

#########

keywords1 = [line for line in open('keywords1.txt')]
resultKeywords1 = checkKeywords("string_to_test",keywords1)
print resultKeywords1

Hi Martin,
It is always helpful to provide python version, operating system version, full error message,
and input/expected output for the code. Now I can tell you are using Python 2.x but
without having any clue what is in keywords1.txt it is impossible to figure out
what the problem might be. Other than using regular expressions that is. :)

Ramit Prasad


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy andcompleteness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
 
I

Ian Kelly

Hi,

I'm new to Python.
Does someone has an idea what's wrong. I tried everything. The only regex that is tested is the last one in a whole list of regex in keywords.txt
Thanks!
Martin

How do you know that it's the only one being tested? Your debugging
statement only prints one that actually matches, not each one that is
tried.
keywords1 = [line for line in open('keywords1.txt')]

Note that each "keyword" will including the trailing newline, if any.
This is probably why you are only seeing the last keyword match:
because it is the only one without a trailing newline.

To remove the newlines, call the str.strip or str.rstrip method on
each line, or use the str.splitlines method to get the keywords:

keywords1 = open('keywords1.txt').read().splitlines()
 
V

Vlastimil Brom

2012/10/23 MartinD. said:
Hi,

I'm new to Python.
Does someone has an idea what's wrong. I tried everything. The only regex that is tested is the last one in a whole list of regex in keywords.txt
Thanks!
Martin


########
def checkKeywords( str, lstKeywords ):

for regex in lstKeywords:
match = re.search(regex, str,re.IGNORECASE)
# If-statement after search() tests if it succeeded
if match:
print match.group() ##just debugging
return match.group() ## 'found!

return

#########

keywords1 = [line for line in open('keywords1.txt')]
resultKeywords1 = checkKeywords("string_to_test",keywords1)
print resultKeywords1

Hi,
just a wild guess, as I don't have access to containing the list of
potentially problematic regex patterns
does:
keywords1 = [line.strip() for line in open('keywords1.txt')]
possibly fix yout problem?
the lines of the file iterator also preserve newlines, which might not
be expected in your keywords, strip() removes (be default) any
starting and tryiling whitespace.

hth,
vbr
 
C

cyberdicks

Stripping the line did it !!!
Thank you very much to all !!!
Cheers! :)
Martin


Le mardi 23 octobre 2012 16:36:44 UTC-4, Vlastimil Brom a écrit :
2012/10/23 MartinD.
Hi,

I'm new to Python.
Does someone has an idea what's wrong. I tried everything. The only regex that is tested is the last one in a whole list of regex in keywords.txt





def checkKeywords( str, lstKeywords ):

for regex in lstKeywords:
match = re.search(regex, str,re.IGNORECASE)
# If-statement after search() tests if it succeeded
if match:
print match.group() ##just debugging
return match.group() ## 'found!
#########

keywords1 = [line for line in open('keywords1.txt')]
resultKeywords1 = checkKeywords("string_to_test",keywords1)



Hi,

just a wild guess, as I don't have access to containing the list of

potentially problematic regex patterns

does:

keywords1 = [line.strip() for line in open('keywords1.txt')]

possibly fix yout problem?

the lines of the file iterator also preserve newlines, which might not

be expected in your keywords, strip() removes (be default) any

starting and tryiling whitespace.



hth,

vbr
 
C

cyberdicks

Stripping the line did it !!!
Thank you very much to all !!!
Cheers! :)
Martin


Le mardi 23 octobre 2012 16:36:44 UTC-4, Vlastimil Brom a écrit :
2012/10/23 MartinD.
Hi,

I'm new to Python.
Does someone has an idea what's wrong. I tried everything. The only regex that is tested is the last one in a whole list of regex in keywords.txt





def checkKeywords( str, lstKeywords ):

for regex in lstKeywords:
match = re.search(regex, str,re.IGNORECASE)
# If-statement after search() tests if it succeeded
if match:
print match.group() ##just debugging
return match.group() ## 'found!
#########

keywords1 = [line for line in open('keywords1.txt')]
resultKeywords1 = checkKeywords("string_to_test",keywords1)



Hi,

just a wild guess, as I don't have access to containing the list of

potentially problematic regex patterns

does:

keywords1 = [line.strip() for line in open('keywords1.txt')]

possibly fix yout problem?

the lines of the file iterator also preserve newlines, which might not

be expected in your keywords, strip() removes (be default) any

starting and tryiling whitespace.



hth,

vbr
 
D

Dennis Lee Bieber

Hi,

I'm new to Python.
Does someone has an idea what's wrong. I tried everything. The only regex that is tested is the last one in a whole list of regex in keywords.txt
Thanks!
Martin


########
def checkKeywords( str, lstKeywords ):

for regex in lstKeywords:
match = re.search(regex, str,re.IGNORECASE)
# If-statement after search() tests if it succeeded
if match:
print match.group() ##just debugging
return match.group() ## 'found!

return

#########

keywords1 = [line for line in open('keywords1.txt')]

Off-hand, your list of "keywords1" still has a newline marker at the
end of each entry...
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top