1

I have a question on Python string operation:

Here's a string in which there are multiple .(dot),

like: "a3a.b1b2b.cccc.ded.f.g"

The question is to find the content before the last .(dot), which is "a3a.b1b2b.cccc.ded.f" in this example (We call it as STRING_BEFORE_LAST_DOT)

and we need to replace STRING_BEFORE_LAST_DOT to be STRING_BEFORE_LAST_DOT + "_Approved". So the result should be: "a3a.b1b2b.cccc.ded.f_Approved.g".

And some more examples:

"a.b" -> "a_Approved.b"
"first.second.c" -> "first.second_Approved.c"

I can def a function to do so, I'm wondering if there's any advance way, like use .replace() to make it happen. Thanks.

3
  • apart from some fancy regex, you can also do this quite easily with split and join. Have you tried anything yourself so far? Commented May 25, 2021 at 9:21
  • @Ma0 Yeah, I tried to def a function (split by dot and reconnect the parts one by one). I know it's far away from a efficient solution, so come to ask for help.. Commented May 25, 2021 at 9:25
  • That should not be too bad actually. Is performance so important for your application? Commented May 25, 2021 at 9:26

6 Answers 6

2

You could use join + rsplit with a limit of 1:

>>> "_Approved.".join("a.b".rsplit(".",1))
'a_Approved.b'
>>> "_Approved.".join("first.second.c".rsplit(".",1))
'first.second_Approved.c'
Sign up to request clarification or add additional context in comments.

Comments

1
# your String
string = 'a3a.b1b2b.cccc.ded.f.g'
# find the last dot character in your string
StringIndex = string.rfind('.')
# create a temporary list of your string and swap the last dot with your preferred string (_Approved.)
temp = list(string)
temp[StringIndex] = '_Approved.'
# replace
string = ''.join(temp)
# print
print('Old String: a3a.b1b2b.cccc.ded.f.g \nNew String: '+ string)

# OUTPUT
# Old String: a3a.b1b2b.cccc.ded.f.g 
# New String: a3a.b1b2b.cccc.ded.f_Approved.g

Comments

0

Here is a solution you can try out,

def _replace(input_):
    sub_ = input_[input_.rfind("."):]
    return input_.replace(sub_, f"_Approved{sub_}")


print(_replace("a.b"))
print(_replace("first.second.c"))

a_Approved.b
first.second_Approved.c

Comments

0

An example of how to do it with a regexp:

import re
s = "a3a.b1b2b.cccc.ded.f.g" 
no_ext = re.split(r'\.[^\.]*$', s)[0]

Comments

0

Here is an alternative. You can access every dot's index with this:

import re
str = "a3a.b1b2b.cccc.ded.f"
# Finds every dot's starting index. (For ending of matched value d.end())
dots = [d.start(0) for d in re.finditer(r'\.+', str)]
# _Approved added
str = str[:dots[-1]] + "_Approved" + str[dots[-1]:]

Comments

0

You can use a pattern to match from the last dot until the end of the string .[^.]*$

Then replace with _Approved and add the full match after it using \g<0>

print(re.sub(r"\.[^.]*$", r"_Approved\g<0>", "a3a.b1b2b.cccc.ded.f.g"))

Output

a3a.b1b2b.cccc.ded.f_Approved.g

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.