Regular Expressions 101

Save & Share

  • Save Regex
    ctrl+s
  • Update Regex
    ctrl+⇧+s

Flavor

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

Function

  • Match
  • Substitution
  • List
  • Unit Tests
/
^((?![!?$]*[^!?$]))?(?:[^!]\2?+(?=(!*)(\\\3?+.(?!\3)))){4}(?(1)|Ha! No one will ever get this one...)|(?!(?1))\Q\1?!($!?)?\E\1?!($!?)?(?<!.{12})\Q(?=(?1))\E(?=(?1))!\?$
/
1st Alternative
^((?![!?$]*[^!?$]))?(?:[^!]\2?+(?=(!*)(\\\3?+.(?!\3)))){4}(?(1)|Ha! No one will ever get this one...)
^ asserts position at start of the string
1st Capturing Group
((?![!?$]*[^!?$]))?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Negative Lookahead
(?![!?$]*[^!?$])
Assert that the Regex below does not match
Match a single character present in the list below
[!?$]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
!?$
matches a single character in the list !?$ (case sensitive)
Match a single character not present in the list below
[^!?$]
!?$
matches a single character in the list !?$ (case sensitive)
Non-capturing group
(?:[^!]\2?+(?=(!*)(\\\3?+.(?!\3)))){4}
{4} matches the previous token exactly 4 times
Match a single character not present in the list below
[^!]
! matches the character ! with index 3310 (2116 or 418) literally (case sensitive)
\2
matches the same text as most recently matched by the 2nd capturing group
?+ matches the previous token between zero and one times, as many times as possible, without giving back (possessive)
Positive Lookahead
(?=(!*)(\\\3?+.(?!\3)))
Assert that the Regex below matches
2nd Capturing Group
(!*)
!
matches the character ! with index 3310 (2116 or 418) literally (case sensitive)
3rd Capturing Group
(\\\3?+.(?!\3))
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
\3
matches the same text as most recently matched by the 3rd capturing group
?+ matches the previous token between zero and one times, as many times as possible, without giving back (possessive)
. matches any character (except for line terminators)
Negative Lookahead
(?!\3)
Assert that the Regex below does not match
\3 matches the same text as most recently matched by the 3rd capturing group
Conditional
(?(1)|Ha! No one will ever get this one...)
Conditionally matches one of two options depending on whether the 1st capturing group matched
If condition is met, match the following regex null, matches any position
Else match the following regex
Ha! No one will ever get this one...
Ha! No one will ever get this one
matches the characters Ha! No one will ever get this one literally (case sensitive)
. matches any character (except for line terminators)
. matches any character (except for line terminators)
. matches any character (except for line terminators)
2nd Alternative
(?!(?1))\Q\1?!($!?)?\E\1?!($!?)?(?<!.{12})\Q(?=(?1))\E(?=(?1))!\?$
Negative Lookahead
(?!(?1))
Assert that the Regex below does not match
(?1) recurses the 1st subpattern
\Q\1?!($!?)?\E matches the characters \1?!($!?)? literally (case sensitive)
\1
matches the same text as most recently matched by the 1st 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 3310 (2116 or 418) literally (case sensitive)
4th Capturing Group
($!?)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
!
matches the character ! with index 3310 (2116 or 418) literally (case sensitive)
Negative Lookbehind
(?<!.{12})
Assert that the Regex below does not match
.
matches any character (except for line terminators)
{12} matches the previous token exactly 12 times
\Q(?=(?1))\E matches the characters (?=(?1)) literally (case sensitive)
Positive Lookahead
(?=(?1))
Assert that the Regex below matches
(?1) recurses the 1st subpattern
! matches the character ! with index 3310 (2116 or 418) literally (case sensitive)
\? matches the character ? with index 6310 (3F16 or 778) literally (case sensitive)
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
Your regular expression does not match the subject string.Try launching the debugger to find out why.

Regular Expression
No Match

/
/

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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html