simplest python equivalent to R's gsub


Is there a simple/one-line python equivalent to R’s gsub function?

strings = c("Important text,      !Comment that could be removed", "Other String")
gsub("(,[ ]*!.*)$", "", strings) 
# [1] "Important text" "Other String"  
Asked By: Deena



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]
Answered By: Nick Becker

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:, 'Here goes', flags=re.I)
Out[13]: 'HERE goes'
Answered By: Benjamin Atkin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.