473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 1641
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

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

Similar topics

8
by: Sharif T. Karim | last post by:
I am trying to do the following with my search script that looks for records in a mysql table. The following is an example of what I am trying to do. Text being searched: -- The brown fox...
3
by: Nel | last post by:
Hi all, I have been searching for some clues as to how to show a substring from a database search. $pagetitle = "Page Title"; $pagetext = "This is <b>a typical</b> string that would be...
2
by: gyromagnetic | last post by:
Hi, I have written a function that searches a text string for various words. The text is searched using a boolean 'and' or a boolean 'or' of the input list of search terms. Since I need to use...
4
by: Ken Fine | last post by:
I'm looking to find or create an ASP script that will take a string, examine it for a search term, and if it finds the search term in the string, return the highlighted search term along with the...
5
by: David | last post by:
Hi, I'm trying to add a search facility to a page that looks for matches in one, other or both memo fields of a database. The code below works fine if the visitor types in one word, or the term...
5
by: Martien van Wanrooij | last post by:
I have been using phpdig in some websites but now I stored a lot of larger texts into a mysql database. In the phpdig search engine, when you entered a search word, the page where the search word...
3
by: Russell | last post by:
Hey, ok i have numerous tables to search through for a 'site search'. some of the searchble fields have html embeded within so after some quick referencing, saw I can use the regExp function...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
6
by: gk | last post by:
Hi, I have trouble figuring out how to search for a particular string in an XML document and annotating it. For example if I'm looking for the string "sentence with tagged words" and wish to...
1
by: nganglove | last post by:
C++ string search -------------------------------------------------------------------------------- Hello, please can any one help me? I am given an assigment in C++ to read a text file and...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.