Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31953 > unrolled thread

regex function driving me nuts

Started by"MartinD." <cyberdicks@gmail.com>
First post2012-10-23 12:51 -0700
Last post2012-10-24 01:27 -0400
Articles 7 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  regex function driving me nuts "MartinD." <cyberdicks@gmail.com> - 2012-10-23 12:51 -0700
    RE: regex function driving me nuts "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-23 20:29 +0000
    Re: regex function driving me nuts Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-23 14:32 -0600
    Re: regex function driving me nuts Vlastimil Brom <vlastimil.brom@gmail.com> - 2012-10-23 22:36 +0200
      Re: regex function driving me nuts cyberdicks@gmail.com - 2012-10-23 16:51 -0700
      Re: regex function driving me nuts cyberdicks@gmail.com - 2012-10-23 16:51 -0700
    Re: regex function driving me nuts Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-24 01:27 -0400

#31953 — regex function driving me nuts

From"MartinD." <cyberdicks@gmail.com>
Date2012-10-23 12:51 -0700
Subjectregex function driving me nuts
Message-ID<0becd1f8-e760-49c1-88d6-1c11b49e203c@googlegroups.com>
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

[toc] | [next] | [standalone]


#31955

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Date2012-10-23 20:29 +0000
Message-ID<mailman.2688.1351024173.27098.python-list@python.org>
In reply to#31953
MartinD 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
> 
> 
> ########
> 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 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.  

[toc] | [prev] | [next] | [standalone]


#31956

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-10-23 14:32 -0600
Message-ID<mailman.2689.1351024383.27098.python-list@python.org>
In reply to#31953
On Tue, Oct 23, 2012 at 1:51 PM, MartinD. <cyberdicks@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()

[toc] | [prev] | [next] | [standalone]


#31957

FromVlastimil Brom <vlastimil.brom@gmail.com>
Date2012-10-23 22:36 +0200
Message-ID<mailman.2690.1351024603.27098.python-list@python.org>
In reply to#31953
2012/10/23 MartinD. <cyberdicks@gmail.com>:
> 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list

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

[toc] | [prev] | [next] | [standalone]


#31976

Fromcyberdicks@gmail.com
Date2012-10-23 16:51 -0700
Message-ID<5d6481e3-b961-4830-9a25-4668b951475b@googlegroups.com>
In reply to#31957
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
> 
> > 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
> 
> >
> 
> > --
> 
> > http://mail.python.org/mailman/listinfo/python-list
> 
> 
> 
> 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

[toc] | [prev] | [next] | [standalone]


#31977

Fromcyberdicks@gmail.com
Date2012-10-23 16:51 -0700
Message-ID<mailman.2704.1351036276.27098.python-list@python.org>
In reply to#31957
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
> 
> > 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
> 
> >
> 
> > --
> 
> > http://mail.python.org/mailman/listinfo/python-list
> 
> 
> 
> 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

[toc] | [prev] | [next] | [standalone]


#31994

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-10-24 01:27 -0400
Message-ID<mailman.2718.1351056598.27098.python-list@python.org>
In reply to#31953
On Tue, 23 Oct 2012 12:51:34 -0700 (PDT), "MartinD."
<cyberdicks@gmail.com> declaimed the following in
gmane.comp.python.general:

> 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...
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web