gsub

gsub(pattern, replacement, x, ignore_case=False, fixed=False)

Pattern replacement in texts

Parameters

Name Type Description Default
pattern str character string containing a regular expression (or character string for fixed = TRUE) to be matched in x required
replacement str a replacement for matched pattern to be put in x required
x Union[str, List[str]] A str or a list of str required
ignore_case bool bool indicating to do case-insensitive matching. Defaults to False. False
fixed bool bool indicating that the pattern is not a regular expression. Defaults to False. False

Returns

Name Type Description
Union[str, List[str]] a list of the same length as x or a str where the provided replacement has been done

Examples

>>> from rlike import *
>>> gsub(pattern = 'Hello', replacement = 'My', x = 'Hello World. hello')
'My World. hello'
>>> gsub(pattern = 'Hello', replacement = 'My', x = 'Hello World. hello', ignore_case = True)
'My World. My'
>>> gsub(pattern = 'Hello', replacement = 'My', x = ['Hello World', 'hello, how are you'], ignore_case = True)
['My World', 'My, how are you']
>>> gsub(pattern = '[0-9]+', replacement = '', x = ['abc.123.xyz', '5432 one liftoff 1.'])
['abc..xyz', ' one liftoff .']