473,770 Members | 4,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to search string for words??

13 New Member
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|me ets’ 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(patte rn, 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 1656
ghostdog74
511 Recognized Expert Contributor
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|me ets’ 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(patte rn, 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 New Member
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
2214
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 jumped over the green fence then jumped into the web monitor. It was hurt so it jumped backwards and fell on its! -- The word we're searching for "web".
3
2283
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 stored<br>in the database.";
2
2399
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 this function for many long strings and many search words, I would like to use as efficient a method as possible. Are there improvements that can be made to the code below? Are there better alternatives?
4
2678
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 words that surround it. In other words, I want the search term highlighted and shown in an excerpt of the context in which it appears. Any suggestions or pointers? This behavior is most often seen as part of a search engine. In my case, I want...
5
2618
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 just happens to exist in one of the queried fields. What I'd really like is for a visitor to type in an expression, or query in the same format as you would use in a search engine and it would find appropriate matches. Any ideas how I can...
5
2495
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 was found was displayed with about 2 lines before and 2 lines behind the search word itself. Let us say you look for "peanut butter" an the word is found in a larger text about sandwiches, even when it is on the 40th line of the text you would get...
3
2027
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 to strip out all the HTML leaving only the raw text. (done and works a treat) My issue is:
0
2080
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 end of this message, but I will start with an overview of the problem. I've made a content management solution for my work with a decently structured relational database system. The CMS stores articles. The CMS also stores related items --...
6
2860
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 annotate it with a "found" tag: original xml: <xml>an example sentence <tag>with tagged words</tag></xml> resulting annotated xml:
1
3273
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 search for certain words.If those stated words are found, then I should print the enire them including the entire line. Below is an example of such a text file Example text file: Possible failures: -> Member=1, FSce=1, MP=13.16%, atching...
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.