simplest python equivalent to Rs gsub

simplest python equivalent to Rs gsub

For a string:

import re
string = Important text,      !Comment that could be removed
re.sub((,[ ]*!.*)$, , string)

Since you updated your question to be a list of strings, you can use a list comprehension.

import re
strings = [Important text,      !Comment that could be removed, Other String]
[re.sub((,[ ]*!.*)$, , x) for x in strings]

gsub is the normal sub in python – that is, it does multiple replacements by default.

The method signature for re.sub is sub(pattern, repl, string, count=0, flags=0)

If you want it to do a single replacement you specify count=1:

In [2]: re.sub(t, s, butter, count=1)
Out[2]: buster

re.I is the flag for case insensitivity:

In [3]: re.sub(here, there, Here goes, flags=re.I)
Out[3]: there goes

You can pass a function that takes a match object:

In [13]: re.sub(here, lambda m: m.group().upper(), Here goes, flags=re.I)
Out[13]: HERE goes

simplest python equivalent to Rs gsub

Leave a Reply

Your email address will not be published. Required fields are marked *