string matching
pattern recognition
regex
string manipulation
pattern matching

Check if string matches pattern

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Checking whether a string matches a pattern can mean different things depending on the problem. Sometimes you want an exact whole-string match, sometimes you only want to know whether the pattern appears anywhere inside the text, and sometimes the pattern is simple enough that regular expressions are unnecessary.

That is why pattern-matching code often feels confusing at first. The real first step is choosing the right kind of match: exact, partial, wildcard-like, or regular expression based.

Exact Match Versus Pattern Match

If the requirement is just equality, use a normal string comparison instead of regex:

python
text = "order-123"
print(text == "order-123")

Regex and other pattern tools are only worth using when the input can vary in a structured way.

For example, if valid values look like order- followed by digits, then a pattern is appropriate.

Whole-String Matching with Regex

A common mistake is using a regex search when the requirement is actually "the entire string must follow this format." In Python, re.fullmatch is a clean way to express that:

python
1import re
2
3pattern = r"order-\d+"
4
5print(bool(re.fullmatch(pattern, "order-123")))
6print(bool(re.fullmatch(pattern, "x order-123")))

The first line returns True, and the second returns False because the second string contains extra text outside the allowed pattern.

If the goal is only to detect whether a pattern appears somewhere inside a larger string, use re.search instead:

python
1import re
2
3pattern = r"order-\d+"
4text = "latest event: order-123 completed"
5
6match = re.search(pattern, text)
7print(match.group(0) if match else "no match")

This is useful for log scanning, free-form text parsing, and diagnostics.

A Practical Example: Username Validation

Suppose you want usernames to contain only letters, digits, and underscores, with lengths from 3 to 16:

python
1import re
2
3USERNAME_RE = re.compile(r"[A-Za-z0-9_]{3,16}")
4
5def is_valid_username(value: str) -> bool:
6    return bool(USERNAME_RE.fullmatch(value))
7
8print(is_valid_username("alice_01"))
9print(is_valid_username("bad-name"))

This is a good example of when regex is the right tool: the rules are structural, compact, and easy to encode.

Do Not Overuse Regex

Not every pattern check needs regex. For a simple prefix, suffix, or containment rule, normal string methods are clearer:

python
1filename = "report.csv"
2
3print(filename.endswith(".csv"))
4print(filename.startswith("report"))
5print("port" in filename)

These are easier to read than regex and usually faster for the specific task.

Escaping and Special Characters

Regex patterns use special characters such as . and *, so literal matching can surprise people. For example, . means "any character," not a real dot.

python
1import re
2
3print(bool(re.fullmatch(r"a.c", "abc")))
4print(bool(re.fullmatch(r"a\.c", "a.c")))

The first pattern accepts any one character in the middle. The second matches an actual period.

Understanding escaping is one of the main differences between working regex and frustrating regex.

Compiling Patterns

If the same pattern is reused many times, compile it once:

python
1import re
2
3EMAIL_RE = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}")
4
5print(bool(EMAIL_RE.fullmatch("[email protected]")))

Compiled regex objects make repeated checks cleaner and avoid rebuilding the same pattern every call.

Common Pitfalls

The biggest pitfall is confusing substring search with whole-string validation. search and fullmatch answer different questions.

Another common issue is forgetting to escape special regex characters. A dot, plus sign, bracket, or question mark can change the meaning of the pattern completely.

Developers also overuse regex when simple string methods would be clearer. If you only need startswith or endswith, use that instead of a full regular expression.

Finally, be careful with patterns copied from the internet. Regex that looks clever but is hard to read is often harder to maintain than a few lines of straightforward validation code.

Summary

  • Decide first whether you need exact equality, substring search, or full pattern validation.
  • Use whole-string matching when the entire value must fit the pattern.
  • Use search-style matching when the pattern only needs to appear somewhere in the text.
  • Prefer simple string methods over regex when the rule is simple.
  • When using regex, pay close attention to escaping and whether the match should be partial or complete.

Course illustration
Course illustration

All Rights Reserved.