HOME
> System software
> Regex: Replace Python - What To Look Out For
Regex: Replace Python - what to look out for
When you use regex in Python, you have several methods available to compare the string with regular expressions. You can also use a special replace method. This makes it possible to replace part of the string with other characters.
These replace methods for regex in Python exist
Regular expressions are used in computer science to examine a string for a certain character pattern. Various programming languages provide you with various methods for this, such as Python. In addition to examining strings for a character string, you can also replace them with other characters. For this, you use a replace method.- In Python, you can use the "sub ()" method to replace certain characters within a string with other characters. Generally, the method is therefore composed of "re.sub (regex, replacement, subject)".
- So you can pass up to three arguments to "sub". As the first argument, you pass the desired pattern, that is, your regular expression. After that, you specify what to replace the found pattern with. Im letzten Argument geben Sie den String an, den Sie überprüfen möchten.
- Anschließend werden alle Substrings innerhalb des Subjects, die mit dem Regex übereinstimmen, durch das Replacement ersetzt.
- Ist ein string = „Computer Bild“, dann könnte der Befehl res = re.sub („[Bb]ild“, „Tipps“, string) lauten. Danach erhalten Sie den String res = „Computer Tipps“.