1

I have a string '1472_1 2014-6-19' and I want to replace whatever number is after the underscore(in this case number one) with the word 'Normal', what I did was to find the index of the element that I want replaced:

print line.replace(line[line.find('_') + 1:line.find(' ')],'Normal', 1)

But instead of getting '1472_Normal 2014-6-19', I got 'Normal472_1 2014-6-19' It seems like my code replace the first 1 instead of the one after the underscore.

I have read this post: Replacing specific words in a string (Python) but still wondering is there a way to specify which element to be replaced instead of using regex?

And to be clear the number after underscore could be any number from 0 to 237

8
  • are you okay with using regex or you want to keep it as string operations? Commented Aug 25, 2015 at 19:18
  • 1
    You have to use regex. Anywhere where a value is not static requires wildcard. Which entails regex Commented Aug 25, 2015 at 19:18
  • Actually I want to avoid regex so I'm wondering is there any other way to do it? Commented Aug 25, 2015 at 19:19
  • 1
    @Enkri_ - It's okay if you don't understand it. I still don't understand some of them that get quite complicated. However, so you have some background this tool should become your best friend. regex101.com Commented Aug 25, 2015 at 19:20
  • 1
    Regex is not required here. Commented Aug 25, 2015 at 19:22

4 Answers 4

3

You could use str.partition():

first, delimiter, second = line.partition('_')
a, s, b = second.partition(' ')
print first + delimiter + 'Normal' + s + b
Sign up to request clarification or add additional context in comments.

5 Comments

Edited to handle multiple digits after underscore (with another partition()).
@TigerhawkT3 still have one question tho, what was the problem with the method I used in the original post?
line[line.find('_') + 1:line.find(' ')] evaluated to just '1', so it replaced the first '1' that it found with 'Normal'.
@TigerhawkT3 Thanks so much
You could fix it by removing the + 1 and adding an underscore before Normal: line.replace(line[line.find('_') :line.find(' ')], '_Normal', 1).
2

no regex

string = '1472_1 2014-6-19'
to_replace = string[string.find('_'):string.find(' ')]
string = string.replace(to_replace,'_Normal')
print string

9 Comments

How do you know there will always be a space after? You don't sir
Beceause the OP is expecting that?
Ha hopefully! Then yes it can be this easy
the string he provided is obviously 'whatever date'. How do you know if there will be space, or no space? You don't sir.
Thanks guys, the data come with a space after
|
2

without regex if there is always a space after the digit which based on line.find(' ') there is, split once to divide the string and rstrip any digits:

s = '1472_1 2014-6-19'

a,b = s.split(None,1)

print("{}Normal {}".format(a.rstrip("0123456789"),b))) 
1472_Normal 2014-6-19  

stripping will work for any amount of digits after the underscore.

With regex:

 import  re

s = '1472_1 2014-6-19'

print(re.sub("(?<=_)\d+","Normal",s))
1472_Normal 2014-6-19

Why your own code fails is because you have a 1 at the start of your string so you replace that not the one after the underscore

1 Comment

OP is specifically asking for a non-regex solution.
1

If you don't want to use RegEx, you could use slicing to isolate the section of the string to replace in. So for instance:

def replace_normal(s)
    ui = s.index('_')  # index of first underscore
    si = s.index(' ')  # index of first space
    return s[:ui+1] + 'Normal' + s[si:]

s1 = '1472_1 2014-6-19'
s2 = '1571_44 2014-7-24'

print replace_normal(s1)  # 1472_Normal 2014-6-19
print replace_normal(s2)  # 1571_Normal 2014-7-24

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.