Search for exact match within a string

This is the place for queries that don't fit in any of the other categories.

Search for exact match within a string

Postby Michelle. » Fri May 31, 2013 8:42 pm

I am importing data from a file using the readlines() function. My objective is to negate arguments, for example "has" needs to turn to "has not" and "has not" needs to turn to "has. I have written a script that works as long as only "has" or only "has not" is in a line:
Code: Select all
if "has not" == "has not" in line:
    line = line.replace("has not", "has")

elif "has" == "has" in line:
    line = line.replace("has" , "has not")

print(line)


My problem, however, is when both of them are in the same line. How would I find the first time that EITHER "has" or "has not" has been used, determine which it is and reverse the argument, and then move onto the next usage of either "has" or "has not"?
Last edited by Yoriz on Fri May 31, 2013 10:04 pm, edited 1 time in total.
Reason: Added code tags to the code
Michelle.
 
Posts: 2
Joined: Fri May 31, 2013 8:33 pm

Re: Search for exact match within a string

Postby Yoriz » Fri May 31, 2013 10:17 pm

One way would be to firstly replace "has not" with something else that will not appear in your line of text, then replace "has" with "has not" and finally replace what you replaced "has not" with, with "has".
The following example will make more sense then my discription.

Code: Select all
line = ('for example "has" needs to turn to "has not" and "has not" needs to '
        'turn to "has"')

print line

line = line.replace("has not", "***")
line = line.replace("has", "has not")
line = line.replace("***", "has")

print(line)

Code: Select all
for example "has" needs to turn to "has not" and "has not" needs to turn to "has"
for example "has not" needs to turn to "has" and "has" needs to turn to "has not"
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1049
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Search for exact match within a string

Postby Michelle. » Fri May 31, 2013 11:20 pm

I had tried this before, and something about the way I had written the code must have been incorrect because trying it again now worked perfectly. Thank you so much

Yoriz wrote:One way would be to firstly replace "has not" with something else that will not appear in your line of text, then replace "has" with "has not" and finally replace what you replaced "has not" with, with "has".
The following example will make more sense then my discription.

Code: Select all
line = ('for example "has" needs to turn to "has not" and "has not" needs to '
        'turn to "has"')

print line

line = line.replace("has not", "***")
line = line.replace("has", "has not")
line = line.replace("***", "has")

print(line)

Code: Select all
for example "has" needs to turn to "has not" and "has not" needs to turn to "has"
for example "has not" needs to turn to "has" and "has" needs to turn to "has not"
Michelle.
 
Posts: 2
Joined: Fri May 31, 2013 8:33 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests