In Python3 I have 3 strings:
string1 = '[Foo] bar1'
string2 = '[foo] bar2'
string3 = 'bar3'
I would like to add prefix "Foo" for each string, except first string that already have it. And replace lower "f" to upper "F" in string2. So, I mean, that result must be:
'[Foo] bar1' -> nothing to do
'[foo] bar2' -> '[Foo] bar2'
'bar3' -> '[Foo] bar3'
CodePudding user response:
You can use startswith
to check if the prefix is there (case insensitive; no regex needed). If so, remove it. In all cases, add the prefix (again), but in its proper capitalisation.
def prefixwith(s, prefix):
if s.upper().startswith(prefix.upper()):
s = s[len(prefix):].lstrip() # Remove existing prefix
return prefix ' ' s
Call as follows:
prefix = '[Foo]'
print(prefixwith('[Foo] bar1', prefix))
print(prefixwith('[foo] bar2', prefix))
print(prefixwith('bar3', prefix))
CodePudding user response:
Here is one way to do it using re.subn(). It's the same as re.sub
, but returns also the number of changes made.
We replace each occurrence of [foo]
and [Foo]
at the beginning of the string, then we look at the number of matches. If it's 0, then we add [Foo]
to the beginning of the string.
import re
data = ['[Foo] bar1', '[foo] bar2', 'bar3']
output = []
for element in data:
new_string, count = re.subn(r'^\[[Ff]oo]', '[Foo]', element)
if not count:
new_string = f"[Foo] {new_string}"
output.append(new_string)
print(output)
Output:
[
'[Foo] bar1',
'[Foo] bar2',
'[Foo] bar3'
]
The regex used is the following:
^\[[Ff]oo]
^
: asserts position at start of a line.\[
: matches[
.[Ff]
: matches eitherf
orF
.oo]
: matchesoo]
CodePudding user response:
You could replace an optionally existing one:
re.sub(r'^(?i:\[Foo] )?', '[Foo] ', string)
Demo:
import re
strings = '[Foo] bar1', '[foo] bar2', 'bar3'
for string in strings:
print(re.sub(r'^(?i:\[foo] )?', '[Foo] ', string))
Output:
[Foo] bar1
[Foo] bar2
[Foo] bar3