How do you replace text in a string using regular expressions? (AI) Flashcards

(14 cards)

1
Q

What is the primary Python re module function for replacing text using a regex pattern?

A

The re.sub(pattern, repl, string, count=0, flags=0) function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does re.sub() do?

A

It finds all occurrences of pattern in string and replaces them with repl.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the three mandatory arguments for re.sub()?

A
  1. pattern (the regex to search for)
  2. repl (the replacement string)
  3. string (the input string to search within)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Example: Replace all occurrences of “old” with “new” in a string.

A

re.sub(r'old', 'new', 'this is old data, very old')
(Result: ‘this is new data, very new’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the count argument in re.sub() for?

A

It specifies the maximum number of pattern occurrences to replace. If 0 (default), all occurrences are replaced.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Example: Replace only the first two occurrences of “apple”.

A

re.sub(r'apple', 'orange', 'apple pie, apple juice, apple tree', count=2)
(Result: ‘orange pie, orange juice, apple tree’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you use capturing groups from the original match in the replacement string?

A

Refer to them in repl using \1, \2, etc., for the first, second, etc., captured groups. (Or \g<name> for named groups).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Example: Reformat “YYYY-MM-DD” to “DD/MM/YYYY” using capturing groups.

A

re.sub(r'(\d{4})-(\d{2})-(\d{2})', r'\3/\2/\1', 'Date: 2023-10-26')
(Result: ‘Date: 26/10/2023’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can the repl argument be a function?

A

Yes. If repl is a function, it’s called for every non-overlapping match, and its return value is used as the replacement string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When using re.compile() for efficiency, how do you perform a substitution?

A

Use the .sub() method on the compiled pattern object:
compiled_pattern.sub(repl, string)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is re.subn()?

A

It’s similar to re.sub(), but it returns a tuple of (new_string, number_of_substitutions).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What happens if the pattern is not found in the string?

A

The original string is returned unchanged.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you make re.sub() case-insensitive?

A

Pass the re.IGNORECASE (or re.I) flag:
re.sub(r'word', 'replaced', 'Word is here', flags=re.IGNORECASE)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Example: Replacing multiple spaces with a single space.

A

re.sub(r'\s+', ' ', 'Hello world !')
(Result: ‘Hello world !’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly