My QA Projects

QA Projects I was involded.

View on GitHub

Pattern Matching - Repetition Qualifiers

Repetition Qualifier Characters

Specific Repetition Qualifiers

{n}
# match exactly n occurrences
{n,}
# match at least n occurrences
{n,m}
# match between n and m occurrences
{,m}
# match between 0 and m occurrences

Repetition Example 1

Pattern String result
\d* (empty string) match
\d* 8 match
\d* 1234 match
\d* 12345 match

Repetition Example 2

Pattern String result
\d? (empty string) match
\d? 8 match
\d? 1234 match
\d? 12345 match

Repetition Example 3

Pattern String result
\d+ (empty string) no match
\d+ 8 match
\d+ 1234 match
\d+ 12345 match

Repetition Example 4

Pattern String result
\d{5} (empty string) no match
\d{5} 8 no match
\d{5} 1234 no match
\d{5} 12345 match

Repetition Example 5

Pattern String result
\d{1,4} (empty string) match
\d{1,4} 8 match
\d{1,4} 1234 match
\d{1,4} 12345 match

Greedy Qualifiers

Greedy Example

import re

s = '<tag>data</tag>'
pattern = '<.*>'
match = re.search(pattern,s)
print(match.group(0))
# Output <tag>data</tag>

Handling Greedy Matching

Non-Greedy Matching

import re

s = '<tag>data</tag>'
pattern = '<.*?>'
match = re.search(pattern,s)
print(match.group(0))
# Output <tag>