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
"
(^ *\S+ *$)|(^ *(\S+ ){1,3}\S+ *$)|(^ *(\S+ ){5,}\S+ *$)|(^ +(\S+ ){4}\S+ +$)
"
mg
1st Alternative
(^ *\S+ *$)
1st Capturing Group
(^ *\S+ *$)
^ asserts position at start of a line
matches the character with index 3210 (2016 or 408) literally (case sensitive)
\S
matches any non-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 3210 (2016 or 408) literally (case sensitive)
$ asserts position at the end of a line
2nd Alternative
(^ *(\S+ ){1,3}\S+ *$)
2nd Capturing Group
(^ *(\S+ ){1,3}\S+ *$)
^ asserts position at start of a line
matches the character with index 3210 (2016 or 408) literally (case sensitive)
3rd Capturing Group
(\S+ ){1,3}
{1,3} matches the previous token between 1 and 3 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
\S
matches any non-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 3210 (2016 or 408) literally (case sensitive)
\S
matches any non-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 3210 (2016 or 408) literally (case sensitive)
$ asserts position at the end of a line
3rd Alternative
(^ *(\S+ ){5,}\S+ *$)
4th Capturing Group
(^ *(\S+ ){5,}\S+ *$)
^ asserts position at start of a line
matches the character with index 3210 (2016 or 408) literally (case sensitive)
5th Capturing Group
(\S+ ){5,}
{5,} matches the previous token between 5 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
\S
matches any non-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 3210 (2016 or 408) literally (case sensitive)
\S
matches any non-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 3210 (2016 or 408) literally (case sensitive)
$ asserts position at the end of a line
4th Alternative
(^ +(\S+ ){4}\S+ +$)
6th Capturing Group
(^ +(\S+ ){4}\S+ +$)
^ asserts position at start of a line
matches the character with index 3210 (2016 or 408) literally (case sensitive)
7th Capturing Group
(\S+ ){4}
{4} matches the previous token exactly 4 times
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
\S
matches any non-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 3210 (2016 or 408) literally (case sensitive)
\S
matches any non-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 3210 (2016 or 408) literally (case sensitive)
$ asserts position at the end of a line
Global pattern flags
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
g modifier: global. All matches (don't return after first match)
Your regular expression does not match the subject string.

Regular Expression
No Match

r"
"
mg

Test String

Code Generator

Generated Code

Loading code sample...
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report.
For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html