469,332 Members | 6,649 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

how to search string for words??

13
Hi I want to search a text file (.txt) for words that I specify in my program. However, I can’t come up with any working solution. My program search string in a text file for ‘met|meeting|meets’ but it returns values from words like “metro”: met-ro. I want it to look only for words ‘met, meeting, etc.’ and I don’t want them to be a part of other words. This is what I have right now:

pattern = re.compile('(?i)met|meeting')
match = re.search(pattern, filestring)
if match:
print 'Found match in ' +filename
print
count = count + 1

else:
print 'Match not found in ' +filename
print

Can anyone give me solution to that problem?
Thx
Mar 20 '07 #1
2 1511
ghostdog74
511 Expert 256MB
Hi I want to search a text file (.txt) for words that I specify in my program. However, I can’t come up with any working solution. My program search string in a text file for ‘met|meeting|meets’ but it returns values from words like “metro”: met-ro. I want it to look only for words ‘met, meeting, etc.’ and I don’t want them to be a part of other words. This is what I have right now:

pattern = re.compile('(?i)met|meeting')
match = re.search(pattern, filestring)
if match:
print 'Found match in ' +filename
print
count = count + 1

else:
print 'Match not found in ' +filename
print

Can anyone give me solution to that problem?
Thx

if you really have to use regular expression, you can use the \b ( match at word boundary) operator. eg

Expand|Select|Wrap|Line Numbers
  1. >>> import re
  2. >>> data=open("file").read()
  3. >>> re.findall(r'\bmet\b|\bmeeting\b|\bmeets\b',data)
  4. ['meeting', 'meets', 'met']
  5.  
sample input:
Expand|Select|Wrap|Line Numbers
  1. metro
  2. comet
  3. meeting
  4. meteor
  5. meetings
  6. meets
  7. meetshere
  8. met
  9.  
however without regular expression, you can use the equality operator "==" to test your search or the "in" keyword
eg only:
Expand|Select|Wrap|Line Numbers
  1. >>> for line in open("file"):
  2. ...  if "meet" == line.strip():  #or if line.strip() in ['meet','meeting','met']
  3. ...    print "found"
  4.  
Mar 20 '07 #2
sovixi
13
thanks it works just how I wanted!!!
Mar 20 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Sharif T. Karim | last post: by
3 posts views Thread by Nel | last post: by
2 posts views Thread by gyromagnetic | last post: by
5 posts views Thread by Martien van Wanrooij | last post: by
6 posts views Thread by gk | last post: by
1 post views Thread by nganglove | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.