eg string +444401608+642055
need to remove any '+' characters unelss the '+' is in the first position of the string.
Prefereably using the 're' python library.
+444401608+642055 -> +444401608642055
+4444+01608+642+055 -> +444401608642055
thanks in advance!!
You could use:
foo = '+444401608+642055'
bar = foo[0] + foo[1:].replace('+', '')
print(bar)
This assumes your string has at least one character ( foo[0]
). It works because if foo[0]
isn't a +
we can safely ignore it in the str.replace()
anyway. foo[1:]
is just the rest of your string from the first character on.
If you really want to use regex you could use:
import re
foo = '+444401608+642055'
bar = re.sub(r'(?<!^)\+', '', foo)
print(bar)
(?<!)
is a negative look behind.
^
indicates the start of a line.
\+
matches the character +
literally.
So in other words, look for every +
character that isn't immediately preceded by a line start and replace with ''
or nothing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.