10 examples of 'python string replace regex' in Python

Every line of 'python string replace regex' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
194def replace(string, old, new):
195 return string.replace(old, new)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

40def regex_replace(value, pattern, replacement, ignorecase=False, multiline=False):
41 """Do a regex search and replace on 'value'"""
42 flags = 0
43 if ignorecase:
44 flags |= re.I
45 if multiline:
46 flags |= re.M
47 compiled_pattern = re.compile(pattern, flags=flags)
48 return compiled_pattern.sub(replacement, str(value))
117def regexReplace(regex,replaceString,inputString,regexOpts):
118 retVal = inputString
119 match = re.search(regex,inputString,regexOpts)
120 if match:
121 retVal = inputString[:match.start(0)] + replaceString + inputString[match.end(0):]
122 return retVal
220def string_replace(state):
221 '''
222 In third string on stack, replaces all occurences of second string with first string
223 '''
224 if len(state.stacks['_string']) > 2:
225 replace_this = state.stacks['_string'].stack_ref(1)
226 with_this = state.stacks['_string'].stack_ref(0)
227 in_this = state.stacks['_string'].stack_ref(2)
228 new_string = in_this.replace(replace_this, with_this)
229 state.stacks['_string'].pop_item()
230 state.stacks['_string'].pop_item()
231 state.stacks['_string'].pop_item()
232 state.stacks['_string'].push_item(new_string)
233 return state
195def RegexEscape(str):
196 return str.replace('\\', '\\\\')
79@register.filter
80def replace(str_to_replace, args):
81 """Performs arbitrary string transformations on template fields.
82
83 If you need to perform a transformation which contains a control character
84 or a set of quotes, you will need to create a custom filter below, because
85 Django does not support escape characters in their filter parameters.
86
87 Args:
88 str_to_replace: the string to be transformed.
89 args: "/old/new" replaces old with new.
90
91 Returns:
92 The string with the specified replacement.
93 """
94 if str_to_replace is None:
95 return None
96 search_str = args.split(args[0])[1]
97 replace_str = args.split(args[0])[2]
98 return str_to_replace.replace(search_str, replace_str)
231def _compile(self, use_regex, flags):
232 super(Replacement, self)._compile(use_regex, flags)
233 precompile_exceptions(self.exceptions, use_regex, flags)
50def replace(s, old, new, maxsplit=None):
51 if maxsplit is None:
52 return s.replace(old, new)
53 else:
54 return s.replace(old, new, maxsplit)
28def replace(self, text):
29 # 过滤特殊字符
30 s = re.sub(r"[^a-zA-Z\d',.!?$:-]+", r" ", text)
31 # 拆分缩写
32 for (pattern, repl) in self.patterns:
33 s = re.sub(pattern, repl, s)
34 # 拆分标点
35 text = []
36 for fragment in s.split():
37 text.extend(re.split(self._WORD_SPLIT, fragment))
38 return ' '.join(text)
4def replace(data, replacements):
5 """
6 Performs several string substitutions on the initial ``data`` string using
7 a list of 2-tuples (old, new) defining substitutions and returns the resulting
8 string.
9 """
10 return reduce(lambda a, kv: a.replace(*kv), replacements, data)

Related snippets