Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
regex function driving me nuts
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
MartinD.  
View profile  
 More options Oct 23 2012, 3:51 pm
Newsgroups: comp.lang.python
From: "MartinD." <cyberdi...@gmail.com>
Date: Tue, 23 Oct 2012 12:51:34 -0700 (PDT)
Local: Tues, Oct 23 2012 3:51 pm
Subject: regex function driving me nuts
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Prasad, Ramit  
View profile  
 More options Oct 23 2012, 4:29 pm
Newsgroups: comp.lang.python
From: "Prasad, Ramit" <ramit.pra...@jpmorgan.com>
Date: Tue, 23 Oct 2012 20:29:10 +0000
Local: Tues, Oct 23 2012 4:29 pm
Subject: RE: regex function driving me nuts

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 and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Kelly  
View profile  
 More options Oct 23 2012, 4:33 pm
Newsgroups: comp.lang.python
From: Ian Kelly <ian.g.ke...@gmail.com>
Date: Tue, 23 Oct 2012 14:32:31 -0600
Local: Tues, Oct 23 2012 4:32 pm
Subject: Re: regex function driving me nuts

On Tue, Oct 23, 2012 at 1:51 PM, MartinD. <cyberdi...@gmail.com> wrote:
> 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()


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Vlastimil Brom  
View profile  
 More options Oct 23 2012, 4:36 pm
Newsgroups: comp.lang.python
From: Vlastimil Brom <vlastimil.b...@gmail.com>
Date: Tue, 23 Oct 2012 22:36:40 +0200
Local: Tues, Oct 23 2012 4:36 pm
Subject: Re: regex function driving me nuts
2012/10/23 MartinD. <cyberdi...@gmail.com>:

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
cyberdi...@gmail.com  
View profile  
 More options Oct 23 2012, 7:51 pm
Newsgroups: comp.lang.python
From: cyberdi...@gmail.com
Date: Tue, 23 Oct 2012 16:51:11 -0700 (PDT)
Local: Tues, Oct 23 2012 7:51 pm
Subject: Re: regex function driving me nuts
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 :


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
cyberdi...@gmail.com  
View profile  
 More options Oct 23 2012, 7:51 pm
Newsgroups: comp.lang.python
From: cyberdi...@gmail.com
Date: Tue, 23 Oct 2012 16:51:11 -0700 (PDT)
Local: Tues, Oct 23 2012 7:51 pm
Subject: Re: regex function driving me nuts
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 :


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »