When should you use regular expression and when should you avoid them? (AI) Flashcards

(12 cards)

1
Q

Good Use Case: Validating simple, structured text.

A

Use regex for patterns like email addresses, phone numbers, zip codes, or hex codes where the format is well-defined.

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

Good Use Case: Finding and replacing text.

A

Use re.sub() when simple .replace() isn’t enough (e.g., replace all 3-digit numbers, not just a specific number).

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

Good Use Case: Parsing semi-structured log files.

A

Use regex to extract specific data (like IP addresses, timestamps, or error codes) from lines in a log file.

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

Good Use Case: Data scraping (with caution).

A

Regex can be used for quick, simple scraping (e.g., finding all href links), but it’s brittle.

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

Good Use Case: Data cleaning.

A

Use regex to normalize data (e.g., remove all punctuation, collapse multiple spaces into one, strip non-numeric characters).

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

When to AVOID: When simple string methods work.

A

Avoid regex if .startswith(), .endswith(), .split(), .find(), or .replace() can do the job. They are faster and much more readable.

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

When to AVOID: Parsing complex, nested formats like HTML or XML.

A

Do not use regex for this. These formats are not “regular.” A single tag change can break your regex. Use a dedicated parser like BeautifulSoup or lxml.

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

When to AVOID: Parsing JSON data.

A

Do not use regex for this. Always use the built-in json module (json.load() or json.loads()).

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

When to AVOID: When the pattern becomes unreadable.

A

If your regex is over 50 characters long and full of nested groups, it’s a sign you need a different approach. It becomes a maintenance nightmare.

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

When to AVOID: When performance is critical and a simple method exists.

A

Simple string operations are almost always faster than a complex regex compilation and search.

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

Key Risk: What is “Catastrophic Backtracking” or ReDoS?

A

A poorly written regex (often with nested quantifiers like (a+)+) can take an exponentially long time to run on certain inputs, causing a Regular Expression Denial of Service (ReDoS).

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

Guiding Principle: Readability vs. Power

A

Regex is powerful but often hard to read and debug. Prioritize the simplest, most readable solution that solves the problem.

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