0

I'm trying to modify certain lines present in a file. I'm searching for a text and replacing it. For example, in the following code, I'm replacing vR33_ALAN with vR33_ALAN*c.

Here's my code for a test case

lines = ['x  = vR32_ALEX - vR33_ALAN; \n',
 'y = vR33_ALAN; \n']

text_to_search = 'vR33_ALAN'
replacement_text = 'vR33_ALAN*c'
for line in lines:
    print(line.replace(text_to_search, replacement_text), end='')

I could succeed in performing the above task. I want to add one more check before replacing the string that matches text_to_search.

I want to replace text_to_search with replacement_text only if a minus - is NOT present proceeding text_to_search.

Example, The output that I obtain is

x  = vR32_ALEX - vR33_ALAN*c;
y = vR33_ALAN*c;

Desired Output:

x  = vR32_ALEX - vR33_ALAN;
y = vR33_ALAN*c;

I'm not sure how to achieve the above. Any suggestions?

1
  • 3
    this is a good case for using regular expressions. I'm not an expert on them, but you should definitely google regular expressions and use the "re" module to do this. Commented Feb 28, 2019 at 4:08

2 Answers 2

2

You can use re.sub with a negative lookbehind pattern:

import re
lines = ['x  = vR32_ALEX - vR33_ALAN; \n',
 'y = vR33_ALAN; \n']
for line in lines:
    print(re.sub(r'(?<!- )vR33_ALAN', 'vR33_ALAN*c', line), end='')

This outputs:

x  = vR32_ALEX - vR33_ALAN; 
y = vR33_ALAN*c; 
Sign up to request clarification or add additional context in comments.

2 Comments

When I remove the space after - i.e. - vR33_ALAN; the output obtained is x = vR32_ALEX -vR33_ALAN*c ;. The desired output is x = vR32_ALEX - vR33_ALAN; . Therefore, I made a small change re.sub(r'(?<!-) and removed the space after -. I could obtain the output. But, how can we check for both cases? i.e irrespective of whether a space is present or not present after the minus sign , I want the output to be x = vR32_ALEX - vR33_ALAN;
I tried using or print(re.sub(r'(?<!- |?<!-)vR33_ALAN', 'vR33_ALAN*c', line), end='').But didn't succeed.Any suggestions?
1

You can perform that with and without the use of regular expressions. You can simply add the '-'character to text_to_searchand use find to search for the new string

lines = ['x  = vR32_ALEX - vR33_ALAN; \n',
 'y = vR33_ALAN; \n']

text_to_search = 'vR33_ALAN'
replacement_text = 'vR33_ALAN*c'

for line in lines:
  if line.find('- '+text_to_search)!=-1:
    print(line)
  else:
    print(line.replace(text_to_search, replacement_text),end='') 

Or you can use the re module as suggested, for that you have to generate a pattern to search, as you're looking for '-'or add text_to_search as before. The (.*)is to specify that it doesn't matter the characters before and after the pattern.

import re 
lines = ['x  = vR32_ALEX - vR33_ALAN; \n',
 'y = vR33_ALAN; \n']

for line in lines:
  if re.match('(.*)'+' - '+'(.*)',line):
    print(line)
  else:
    print(line.replace(text_to_search, replacement_text),end='')  

The pattern '(.*)'+' - '+text_to_search+'(.*)'should also work. Hope it helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.