Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8

Function

  • Match
  • Substitution
  • List
  • Unit Tests
"
(^(\d+%((?!\r\n)\s+))(\[((\w+)((\-\w+)?((?!\r\n)\s+)?(\w+)?((?!\r\n)\s+)?(\d+)?(\,)?(\d+)?(\.)?(\d?)((?!\r\n)\s+)?(k?B)?)?((?!\r\n)\s+)?(\w+)?\]))?(((((?!\r\n)\s+)\[)(.+)\])*(((?!\r\n)\s+)(\[)(.+))?(\r?\n)))
"
gm
1st Capturing Group
(^(\d+%((?!\r\n)\s+))(\[((\w+)((\-\w+)?((?!\r\n)\s+)?(\w+)?((?!\r\n)\s+)?(\d+)?(\,)?(\d+)?(\.)?(\d?)((?!\r\n)\s+)?(k?B)?)?((?!\r\n)\s+)?(\w+)?\]))?(((((?!\r\n)\s+)\[)(.+)\])*(((?!\r\n)\s+)(\[)(.+))?(\r?\n)))
^ asserts position at start of a line
2nd Capturing Group
(\d+%((?!\r\n)\s+))
\d
matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
% matches the character % with index 3710 (2516 or 458) literally (case sensitive)
3rd Capturing Group
((?!\r\n)\s+)
Negative Lookahead
(?!\r\n)
Assert that the Regex below does not match
\r matches a carriage return (ASCII 13)
\n matches a line-feed (newline) character (ASCII 10)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v  ])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
4th Capturing Group
(\[((\w+)((\-\w+)?((?!\r\n)\s+)?(\w+)?((?!\r\n)\s+)?(\d+)?(\,)?(\d+)?(\.)?(\d?)((?!\r\n)\s+)?(k?B)?)?((?!\r\n)\s+)?(\w+)?\]))?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
5th Capturing Group
((\w+)((\-\w+)?((?!\r\n)\s+)?(\w+)?((?!\r\n)\s+)?(\d+)?(\,)?(\d+)?(\.)?(\d?)((?!\r\n)\s+)?(k?B)?)?((?!\r\n)\s+)?(\w+)?\])
6th Capturing Group
(\w+)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
7th Capturing Group
((\-\w+)?((?!\r\n)\s+)?(\w+)?((?!\r\n)\s+)?(\d+)?(\,)?(\d+)?(\.)?(\d?)((?!\r\n)\s+)?(k?B)?)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
8th Capturing Group
(\-\w+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
9th Capturing Group
((?!\r\n)\s+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Negative Lookahead
(?!\r\n)
Assert that the Regex below does not match
\r matches a carriage return (ASCII 13)
\n matches a line-feed (newline) character (ASCII 10)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v  ])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
10th Capturing Group
(\w+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
11th Capturing Group
((?!\r\n)\s+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Negative Lookahead
(?!\r\n)
Assert that the Regex below does not match
\r matches a carriage return (ASCII 13)
\n matches a line-feed (newline) character (ASCII 10)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v  ])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
12th Capturing Group
(\d+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\d
matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
13th Capturing Group
(\,)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\, matches the character , with index 4410 (2C16 or 548) literally (case sensitive)
14th Capturing Group
(\d+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\d
matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
15th Capturing Group
(\.)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
16th Capturing Group
(\d?)
\d
matches a digit (equivalent to [0-9])
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
17th Capturing Group
((?!\r\n)\s+)?
18th Capturing Group
(k?B)?
19th Capturing Group
((?!\r\n)\s+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Negative Lookahead
(?!\r\n)
Assert that the Regex below does not match
\r matches a carriage return (ASCII 13)
\n matches a line-feed (newline) character (ASCII 10)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v  ])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
20th Capturing Group
(\w+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\] matches the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
21st Capturing Group
(((((?!\r\n)\s+)\[)(.+)\])*(((?!\r\n)\s+)(\[)(.+))?(\r?\n))
22nd Capturing Group
((((?!\r\n)\s+)\[)(.+)\])*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
23rd Capturing Group
(((?!\r\n)\s+)\[)
24th Capturing Group
((?!\r\n)\s+)
Negative Lookahead
(?!\r\n)
Assert that the Regex below does not match
\r matches a carriage return (ASCII 13)
\n matches a line-feed (newline) character (ASCII 10)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v  ])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
25th Capturing Group
(.+)
.
matches any character (except for line terminators)
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\] matches the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
26th Capturing Group
(((?!\r\n)\s+)(\[)(.+))?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
27th Capturing Group
((?!\r\n)\s+)
Negative Lookahead
(?!\r\n)
Assert that the Regex below does not match
\r matches a carriage return (ASCII 13)
\n matches a line-feed (newline) character (ASCII 10)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v  ])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
28th Capturing Group
(\[)
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
29th Capturing Group
(.+)
.
matches any character (except for line terminators)
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
30th Capturing Group
(\r?\n)
\r
matches a carriage return (ASCII 13)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\n matches a line-feed (newline) character (ASCII 10)
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Your regular expression does not match the subject string.

Regular Expression
No Match

r"
"
gm

Test String