Test those two simple pages that uses regex:
Regex101
You need to match HTML tags. (In general case it’s a bad idea to use regex for HTML parsing, but it’s a good learning example).
.*?
, or limit scope [^>]
. Try both, add text inside/after the tag, see step count changesRegex101
Optimize regex that finds certain CSS classes related to product: product-size, product-column, product-info
and product ids that has digits 1,2,3.
Regex101
Watch the step count and the debugger while doing those changes:
a
at the beginningb
Regex101
Watch the step count and the debugger while doing those changes:
a
at the beginningb
, add more a
.Regex101
You have a regex matching simple arithmetic operations. Allowed: two numbers
separated with plus or minus sign, ending with equals sign, e.g. 12+34=
or 32121-23=
12+322-1=
).12+322-1+223-2323+...=
).When you’re done, go to the editable regex demo page and see impact of hastily written regex on user experience. To see timing stats, open browser console. Optimize the regex (you can start in Regex101 and apply it to the demo page).
Regex101
You have a regex matching 6th column (Tea
) in CSV file (again, there are better ways to parse CSV…). It’s rather slow even for valid input. Optimize it.
Regex101
Analyse regex that stopped Cloudflare servers. Optimize it to remove catastrophic backtracking.
Regex101
Analyse regex found by Sam Saffron at code written by a Discourse client. It replaces space before certain characters by HTML thin space (French typography). See how it performs on unexpected input. Optimize it.
Regex101
Analyze regex used in a Microsoft project to match Windows username. Find an input causing catastrophic bactracking. Optimize it.
X*
greedy quantifier (match as much as possible)X*?
lazy quantifier (match as little as possible)X*+
, X++
possessive quantifiers (don’t backtrack)X{n}
repeat n times[XYZ]
character set, [^XYZ]
negated character set\d
digit\w
word character (digit, letter, underscore)^
beginning of the line(?:...)
non-capturing group.* => [^X]*
.*? => [^X]*
(pre-d1|pre-e2|...) => pre-(d1|e2|...)
(,|;|\.) => [,.;]
(A+|B?)+ => A+(BA+)*
(unrolling the loop)(A+|B?)+ => (A+B)*A+
(unrolling the loop)(A+|B?)+ => (A++|B?)+
(avoiding backtracking by using possessive quantifiers)