I want to replace 'nf' with 1.0, so that the resulting string should be:
'(1.0+nfpermult+1.0)'

I tried various approaches using find(), re.sub but I am not able to make it work.

eg. re.sub('nf', '1.0', str) replaces all occurrences of nf. also re.sub('\bnf\b', '1.0', str) does
not work either as 'nf' can be anywhere in the string.

any ideas?

- Kaushik

Recommended Answers

All 12 Replies

that replaces all occurrences of 'nf'. I want to replace only whole words.

Editor's note:
Please do not make your question a moving target!
It is very frustrating to the people who are trying to help you.
Try to state your question in it's full scope the first time around.

string.replace(" nf ","1.0")?

Your question is not clear because you show the after but not the before.

"\bnf\b"

Well, if you want it to be:
'(1.0+nfpermult+1.0)'
Then it appears that 'nf' will be at both the beginning and end, so I'll assume that the original is:
'nf+nfpermult+nf'
so, you do this:

a = 'nf+nfpermult+nf'
a = [2:-2]
b = '1.0'+a+'1.0'

Seems the easiest way to do it for me

Rather tedious, but one more way.

nf_str = 'nf+nfpermult+nf+nf'
ctr = 0
index = -1

go_on = True
while go_on:
   print nf_str
   before = False
   after = False

   index = nf_str.find('nf', index+1)
   if index > -1:
      ## check character before
      if (index == 0) or (not nf_str[index-1].isalpha()):
         before = True

      ## check character after
      if (index+2 >= len(nf_str)) or \
         (not nf_str[index+2].isalpha()):
         after = True

      if before and after:   ## neither is alpha
         nf_str = nf_str[:index] + "1.0" + nf_str[index+2:]

   else:          ## 'nf' not found
      go_on = False
   ctr += 1

print nf_str 

"""  my results
nf+nfpermult+nf+nf
1.0+nfpermult+nf+nf
1.0+nfpermult+nf+nf
1.0+nfpermult+1.0+nf
1.0+nfpermult+1.0+1.0
1.0+nfpermult+1.0+1.0
"""

>>> a = 'nf+nfpermult+nf'
>>> a=a.split("+")
>>> for n,i in enumerate( a ):
... if i == "nf": a[n]="1.0"
...
>>> print '+'.join(a)
1.0+nfpermult+1.0

Just one more tricky way ...

a = '(nf+nfpermult+nf)'

# uses '~' for temporary '+' substitute
x = a.replace('nf+', '1.0~').replace('+nf', '~1.0').replace('~', '+')

print( x )  # (1.0+nfpermult+1.0)

Does nobody read my posts? \b matches the word boundary. A regexp '\bnf\b' does exactly what the OP wants.

Does nobody read my posts? \b matches the word boundary. A regexp '\bnf\b' does exactly what the OP wants.

We do read the original post.

also re.sub('\bnf\b', '1.0', str) does not work either as 'nf' can be anywhere in the string.

And as we do not know the before string, or whether or not there is always a "+" associated with it, we have to make assumptions.

We do read the original post. And as we do not know the before string, or whether or not there is always a "+" associated with it, we have to make assumptions.

Please forgive my English. OP implied Original Poster, who, in the next message says:

I want to replace only whole words.

Not much room for assumption in my opinion.

It all boils down to to a poorly worded question that suddenly includes a moving target. Unless the OP at his/her leisure gives us a correction, we can only assume that ...
original = '(nf+nfpermult+nf)'
result = '(1.0+nfpermult+1.0)'

God only knows what this person means with whole words.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.