Cara menggunakan python regex replace all

Specify the old string print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 6 for the first argument and the new string print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 7 for the second argument.

s = 'one two one two one' print(s.replace(' ', '-')) # one-two-one-two-one

source:

You can remove print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 6 by specifying print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 7 as the empty string print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 3.

print(s.replace(' ', '')) # onetwoonetwoone

source:

Specify the maximum count of replacements: print(s.replace('one', 'XXX')) # XXX two XXX two XXX print(s.replace('one', 'XXX', 2)) # XXX two XXX two one 9

You can specify the maximum number of replacements in the third parameter, print(s.replace('one', 'XXX')) # XXX two XXX two XXX print(s.replace('one', 'XXX', 2)) # XXX two XXX two one 9. If print(s.replace('one', 'XXX')) # XXX two XXX two XXX print(s.replace('one', 'XXX', 2)) # XXX two XXX two one 9 is given, only the first print(s.replace('one', 'XXX')) # XXX two XXX two XXX print(s.replace('one', 'XXX', 2)) # XXX two XXX two one 9 occurrences are replaced.

print(s.replace('one', 'XXX')) # XXX two XXX two XXX print(s.replace('one', 'XXX', 2)) # XXX two XXX two one

source:

Replace multiple different substrings

To replace multiple different strings with the same string, use regular expressions as described below.

There is no method to replace multiple different strings with different ones, but you can apply print(s.replace('one', 'XXX')) # XXX two XXX two XXX print(s.replace('one', 'XXX', 2)) # XXX two XXX two one 8 repeatedly.

print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX

source:

It just calls print(s.replace('one', 'XXX')) # XXX two XXX two XXX print(s.replace('one', 'XXX', 2)) # XXX two XXX two one 8 in order, so if the first print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 7 contains the following print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 6, the first print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 7 is also replaced.

print(s.replace('one', 'XtwoX').replace('two', 'YYY')) # XYYYX YYY XYYYX YYY XYYYX print(s.replace('two', 'YYY').replace('one', 'XtwoX')) # XtwoX YYY XtwoX YYY XtwoX

source:

To replace multiple characters (strings of length print(s.replace('one', 'two').replace('two', 'one')) # one one one one one 0), you can use the print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 0 method described below.

Swap strings

If you want to swap two strings, replacing them in order as described above may not work.

print(s.replace('one', 'two').replace('two', 'one')) # one one one one one

source:

First, you should replace it with another string.

print(s.replace('one', 'X').replace('two', 'one').replace('X', 'two')) # two one two one two

source:

This operation can be turned into a function as follows:

def swap_str(s_org, s1, s2, temp='*q@w-e~r^'): return s_org.replace(s1, temp).replace(s2, s1).replace(temp, s2) print(swap_str(s, 'one', 'two')) # two one two one two

source:

Note that this function does not work if the temporary string print(s.replace('one', 'two').replace('two', 'one')) # one one one one one 2 is included in the original string. If you want to make it strict, you need to check if the temporary string print(s.replace('one', 'two').replace('two', 'one')) # one one one one one 2 is included in the original string, and if so, use a different string for print(s.replace('one', 'two').replace('two', 'one')) # one one one one one 2. In the example above, print(s.replace('one', 'two').replace('two', 'one')) # one one one one one 2 is set to a string of no particular meaning.

To swap multiple characters (strings of length print(s.replace('one', 'two').replace('two', 'one')) # one one one one one 0), you can use the print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 0 method described below.

Replace newline character

If the string contains only one kind of newline character, you can specify it as the first argument of print(s.replace('one', 'XXX')) # XXX two XXX two XXX print(s.replace('one', 'XXX', 2)) # XXX two XXX two one 8.

s_lines = 'one\ntwo\nthree' print(s_lines) # one # two # three print(s_lines.replace('\n', '-')) # one-two-three

source:

Be careful if print(s.replace('one', 'two').replace('two', 'one')) # one one one one one 9 (LF, used in Unix OS including Mac) and print(s.replace('one', 'X').replace('two', 'one').replace('X', 'two')) # two one two one two 0 (CR + LF, used in Windows OS) are mixed. Since print(s.replace('one', 'two').replace('two', 'one')) # one one one one one 9 is included in print(s.replace('one', 'X').replace('two', 'one').replace('X', 'two')) # two one two one two 0, you cannot get the desired result depending on the order.

You can use print(s.replace('one', 'X').replace('two', 'one').replace('X', 'two')) # two one two one two 3, which returns a list split with various newline characters, and print(s.replace('one', 'X').replace('two', 'one').replace('X', 'two')) # two one two one two 4, which combines lists with strings. This way is safe and recommended, especially if you do not know what newline characters are included.

print(s_lines_multi.splitlines()) # ['one', 'two', 'three'] print('-'.join(s_lines_multi.splitlines())) # one-two-three

source:

See the following article for other operations related to line breaks in strings.

  • Handle line breaks (newlines) in Python

Replace multiple different characters: print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 0

Basic usage

Use the print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 0 method to replace multiple different characters. You can create the translation table specified in print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 0 by the print(s.replace('one', 'X').replace('two', 'one').replace('X', 'two')) # two one two one two 8.

Specify a dictionary whose key is the old character and whose value is the new string in the print(s.replace('one', 'X').replace('two', 'one').replace('X', 'two')) # two one two one two 8.

The old character must be a character (a string of length print(s.replace('one', 'two').replace('two', 'one')) # one one one one one 0). The new string is a string or def swap_str(s_org, s1, s2, temp='*q@w-e~r^'): return s_org.replace(s1, temp).replace(s2, s1).replace(temp, s2) print(swap_str(s, 'one', 'two')) # two one two one two 1, where def swap_str(s_org, s1, s2, temp='*q@w-e~r^'): return s_org.replace(s1, temp).replace(s2, s1).replace(temp, s2) print(swap_str(s, 'one', 'two')) # two one two one two 1 removes old characters.

print(s.replace(' ', '')) # onetwoonetwoone 0

source:

The first argument is a string in which old characters are concatenated, the second is a string in which new characters are concatenated, and the third is a string in which characters to be deleted are concatenated. The third argument is optional.

print(s.replace(' ', '')) # onetwoonetwoone 1

source:

In this case, the lengths of the first and second arguments must match.

print(s.replace(' ', '')) # onetwoonetwoone 2

source:

Swap characters

You can swap characters with print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 0.

print(s.replace(' ', '')) # onetwoonetwoone 3

source:

Sponsored Link

Replace by regex: print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 1, print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 2

If you want to replace a string that matches a regular expression (regex) instead of a perfect match, use the def swap_str(s_org, s1, s2, temp='*q@w-e~r^'): return s_org.replace(s1, temp).replace(s2, s1).replace(temp, s2) print(swap_str(s, 'one', 'two')) # two one two one two 6 of the re module.

Basic usage

In print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 1, specify a regex pattern in the first argument, a new string in the second, and a string to be processed in the third.

print(s.replace(' ', '')) # onetwoonetwoone 4

source:

As with print(s.replace('one', 'XXX')) # XXX two XXX two XXX print(s.replace('one', 'XXX', 2)) # XXX two XXX two one 8, you can specify the maximum count of replacements in the fourth parameter, print(s.replace('one', 'XXX')) # XXX two XXX two XXX print(s.replace('one', 'XXX', 2)) # XXX two XXX two one 9.

print(s.replace(' ', '')) # onetwoonetwoone 5

source:

You can also create a regular expression pattern object with s_lines = 'one\ntwo\nthree' print(s_lines) # one # two # three print(s_lines.replace('\n', '-')) # one-two-three 0 and call the def swap_str(s_org, s1, s2, temp='*q@w-e~r^'): return s_org.replace(s1, temp).replace(s2, s1).replace(temp, s2) print(swap_str(s, 'one', 'two')) # two one two one two 6 method. This is more efficient if you want to use the same regular expression pattern repeatedly.

print(s.replace(' ', '')) # onetwoonetwoone 6

source:

Replace multiple substrings with the same string

The following two are useful to remember even if you are unfamiliar with the regex.

Enclose a string with s_lines = 'one\ntwo\nthree' print(s_lines) # one # two # three print(s_lines.replace('\n', '-')) # one-two-three 2 to match any single character in it. You can replace multiple different characters with the same string.

print(s.replace(' ', '')) # onetwoonetwoone 7

source:

If patterns are delimited by s_lines = 'one\ntwo\nthree' print(s_lines) # one # two # three print(s_lines.replace('\n', '-')) # one-two-three 3, it matches any pattern. Of course, it is possible to use special characters of regular expression for each pattern, but it is OK even if normal string is specified as it is. You can replace multiple different strings with the same string.

print(s.replace(' ', '')) # onetwoonetwoone 8

source:

Replace using the matched part

If part of the pattern is enclosed in s_lines = 'one\ntwo\nthree' print(s_lines) # one # two # three print(s_lines.replace('\n', '-')) # one-two-three 4, you can use a string that matches the part enclosed in s_lines = 'one\ntwo\nthree' print(s_lines) # one # two # three print(s_lines.replace('\n', '-')) # one-two-three 4 in the new string.

print(s.replace(' ', '')) # onetwoonetwoone 9

source:

It is necessary to escape s_lines = 'one\ntwo\nthree' print(s_lines) # one # two # three print(s_lines.replace('\n', '-')) # one-two-three 6 like s_lines = 'one\ntwo\nthree' print(s_lines) # one # two # three print(s_lines.replace('\n', '-')) # one-two-three 7 in a normal string (print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 3 or s_lines = 'one\ntwo\nthree' print(s_lines) # one # two # three print(s_lines.replace('\n', '-')) # one-two-three 9), but you can write print(s_lines_multi.splitlines()) # ['one', 'two', 'three'] print('-'.join(s_lines_multi.splitlines())) # one-two-three 0 in a raw string (print(s_lines_multi.splitlines()) # ['one', 'two', 'three'] print('-'.join(s_lines_multi.splitlines())) # one-two-three 1 or print(s_lines_multi.splitlines()) # ['one', 'two', 'three'] print('-'.join(s_lines_multi.splitlines())) # one-two-three 2).

  • Raw strings in Python

You can specify a function, that takes a match object as its argument, as the second argument of def swap_str(s_org, s1, s2, temp='*q@w-e~r^'): return s_org.replace(s1, temp).replace(s2, s1).replace(temp, s2) print(swap_str(s, 'one', 'two')) # two one two one two 6. This allows for more complex operations.

print(s.replace('one', 'XXX')) # XXX two XXX two XXX print(s.replace('one', 'XXX', 2)) # XXX two XXX two one 0

source:

You may use the lambda expression.

  • Lambda expressions in Python

print(s.replace('one', 'XXX')) # XXX two XXX two XXX print(s.replace('one', 'XXX', 2)) # XXX two XXX two one 1

source:

Get the count of replaced parts

print(s.replace('one', 'XXX').replace('two', 'YYY')) # XXX YYY XXX YYY XXX 2 returns a tuple of the replaced string and the number of parts replaced.

print(s.replace('one', 'XXX')) # XXX two XXX two XXX print(s.replace('one', 'XXX', 2)) # XXX two XXX two one 2

source:

The usage of print(s_lines_multi.splitlines()) # ['one', 'two', 'three'] print('-'.join(s_lines_multi.splitlines())) # one-two-three 5 is the same as def swap_str(s_org, s1, s2, temp='*q@w-e~r^'): return s_org.replace(s1, temp).replace(s2, s1).replace(temp, s2) print(swap_str(s, 'one', 'two')) # two one two one two 6. You can use the part grouped by s_lines = 'one\ntwo\nthree' print(s_lines) # one # two # three print(s_lines.replace('\n', '-')) # one-two-three 4 or specify the maximum number of replacements.

print(s.replace('one', 'XXX')) # XXX two XXX two XXX print(s.replace('one', 'XXX', 2)) # XXX two XXX two one 3

source:

Replace by position: slice

There is no method to replace the string at the specified position.

By splitting the string with a slice and concatenating them with another string, you can create a new string with the specified position replaced.

print(s.replace('one', 'XXX')) # XXX two XXX two XXX print(s.replace('one', 'XXX', 2)) # XXX two XXX two one 4

source:

The length of the string (number of characters) can be obtained with print(s_lines_multi.splitlines()) # ['one', 'two', 'three'] print('-'.join(s_lines_multi.splitlines())) # one-two-three 8, so it can be written as follows:

  • Get the length of a string (number of characters) in Python

print(s.replace('one', 'XXX')) # XXX two XXX two XXX print(s.replace('one', 'XXX', 2)) # XXX two XXX two one 5

source:

The number of characters does not have to match, as it just concatenates different strings between the split strings.

Modul apa dalam python untuk menjalankan RegEx?

Modul RegEx Python memiliki paket bawaan yang disebut re , yang dapat digunakan untuk bekerja dengan Ekspresi Reguler.

Apa itu RegEx di Python?

Regex merupakan singkatan dari Regular Expression yang merupakan serangkaian karakter yang mendefinisikan sebuah pola pencarian. Beberapa bidang yang menggunakan metode ini adalah seperti Natural Language Processing (NLP), Text Mining, Data Validation, Finding Pattern, Anomaly Detection dan lainnya.

Postingan terbaru

LIHAT SEMUA