0

I have the following matched strings:

punctacros="Tasla"_TONTA  
punctacros="Tasla"_SONTA  
punctacros="Tasla"_JONTA  
punctacros="Tasla"_BONTA

I want to replace only a part (before the underscore) of the matched strings, and the rest of it should remain the same in each original string.

The result should look like this:

TROGA_TONTA  
TROGA_SONTA  
TROGA_JONTA  
TROGA_BONTA
2
  • What is a matched string? And why do you have quotes in the middle of them? Commented Dec 6, 2013 at 16:54
  • matched means that my regex picked them from a body of text. Quotes are just literal characters in the string. Commented Dec 6, 2013 at 16:56

3 Answers 3

1

Edit:

This should work:

from re import sub
with open("/path/to/file") as myfile:
    lines = []
    for line in myfile:
        line = sub('punctacros="Tasla"(_.*)', r'TROGA\1', line)
        lines.append(line)
with open("/path/to/file", "w") as myfile:
    myfile.writelines(lines)

Result:

TROGA_TONTA  
TROGA_SONTA  
TROGA_JONTA  
TROGA_BONTA

Note however, if your file is exactly like the sample given, you can replace the re.sub line with this:

line = "TROGA_"+line.split("_", 1)[1]

eliminating the need of Regex altogether. I didn't do this though because you seem to want a Regex solution.

Sign up to request clarification or add additional context in comments.

3 Comments

The thing is that i have to first find these kinds of strings in the text and then have to replace them with a new string, keeping parts (after underscore) of them unchanged. Therefore, it has to be done by a regex engine which first finds them and then replace them.
I have to first find them in a text file and then replace them. I am not going to extract them out of the tex. They will remain in the text file after replacing their parts.
@Coddy - Oh, you should have mentioned that in your question. See my edit. Is that what you want?
0
mystring.replace('punctacross="Tasla"', 'TROGA_')

where mystring is string with those four lines. It will return string with replaced values.

2 Comments

I have to first find them in a text file and then replace it. I am not going to extract them out of the tex. They will remain in the text file after replacing their parts.
Yes, you will. Strings are immutable, replace() method will return new string with replaced parts, without modifying your file or original string.
0

If you want to replace everything before the first underscore, try this:

#! /usr/bin/python3

data = ['punctacros="Tasla"_TONTA',
'punctacros="Tasla"_SONTA',  
'punctacros="Tasla"_JONTA',  
'punctacros="Tasla"_BONTA',
'somethingelse!="Tucku"_CONTA']

for s in data:
    print('TROGA' + s[s.find('_'):])

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.