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
/
\[(\[?)(embed|wp_caption|caption|gallery|playlist|audio|video|code_snippet|democracy|democracy_archives|download)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)
/
gm
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
1st Capturing Group
(\[?)
\[
matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
2nd Capturing Group
(embed|wp_caption|caption|gallery|playlist|audio|video|code_snippet|democracy|democracy_archives|download)
1st Alternative
embed
embed
matches the characters embed literally (case sensitive)
2nd Alternative
wp_caption
wp_caption
matches the characters wp_caption literally (case sensitive)
3rd Alternative
caption
caption
matches the characters caption literally (case sensitive)
4th Alternative
gallery
gallery
matches the characters gallery literally (case sensitive)
5th Alternative
playlist
playlist
matches the characters playlist literally (case sensitive)
6th Alternative
audio
audio
matches the characters audio literally (case sensitive)
7th Alternative
video
video
matches the characters video literally (case sensitive)
8th Alternative
code_snippet
code_snippet
matches the characters code_snippet literally (case sensitive)
9th Alternative
democracy
democracy
matches the characters democracy literally (case sensitive)
10th Alternative
democracy_archives
democracy_archives
matches the characters democracy_archives literally (case sensitive)
11th Alternative
download
Negative Lookahead
(?![\w-])
Assert that the Regex below does not match
Match a single character present in the list below
[\w-]
\w matches any word character (equivalent to [a-zA-Z0-9_])
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
3rd Capturing Group
([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)
Match a single character not 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 the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
Non-capturing group
(?:\/(?!\])[^\]\/]*)*?
*? matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
Negative Lookahead
(?!\])
Assert that the Regex below does not match
\] matches the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
Match a single character not 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 the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
Non-capturing group
(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)
1st Alternative
(\/)\]
4th Capturing Group
(\/)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\] matches the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
2nd Alternative
\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?
\] matches the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
Non-capturing group
(?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
5th Capturing Group
([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)
Match a single character not present in the list below
[^\[]
*+ matches the previous token between zero and unlimited times, as many times as possible, without giving back (possessive)
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
Non-capturing group
(?:\[(?!\/\2\])[^\[]*+)*+
*+ matches the previous token between zero and unlimited times, as many times as possible, without giving back (possessive)
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
Negative Lookahead
(?!\/\2\])
Assert that the Regex below does not match
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\2 matches the same text as most recently matched by the 2nd capturing group
\] matches the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
Match a single character not present in the list below
[^\[]
*+ matches the previous token between zero and unlimited times, as many times as possible, without giving back (possessive)
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\2 matches the same text as most recently matched by the 2nd capturing group
\] matches the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
6th Capturing Group
(\]?)
\]
matches the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
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.Try launching the debugger to find out why.

Regular Expression
No Match

/
/
gm

Test String