Regular Expressions 101

@regex101
Donate
Sponsor
Contact
Bug Reports & Feedback
Wiki
Whats new?

Save & Share

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

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python 2.7
  • 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!($!)?(?<!.)\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 ! 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
(!)
3rd Capturing Group
(\\\3.(?!\3))
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!($!)?(?<!.)\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 ! 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 ! literally (case sensitive)
Negative Lookbehind
(?<!.)
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 ! literally (case sensitive)
\? matches the character ? 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