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
/
{[ \t]*foreach[ \t]*\$(_*[A-Za-z]+[A-Za-z0-9_]*)[ \t]*as[ \t]*\$(_*[A-Za-z]+[A-Za-z0-9_]*)(?:[ \t]=>[ \t]*\$(_*[A-Za-z]+[A-Za-z0-9_]*))?[ \t]*}(.*){[ \t]*\/foreach[ \t]*}
/
Uis
{ matches the character { with index 12310 (7B16 or 1738) literally (case insensitive)
Match a single character present in the list below
[ \t]
* matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
matches the character with index 3210 (2016 or 408) literally (case insensitive)
\t matches a tab character (ASCII 9)
foreach
matches the characters foreach literally (case insensitive)
Match a single character present in the list below
[ \t]
* matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
matches the character with index 3210 (2016 or 408) literally (case insensitive)
\t matches a tab character (ASCII 9)
\$ matches the character $ with index 3610 (2416 or 448) literally (case insensitive)
1st Capturing Group
(_*[A-Za-z]+[A-Za-z0-9_]*)
_
matches the character _ with index 9510 (5F16 or 1378) literally (case insensitive)
Match a single character present in the list below
[A-Za-z]
+ matches the previous token between one and unlimited times, as few times as possible, expanding as needed (lazy)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
Match a single character present in the list below
[A-Za-z0-9_]
* matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
_ matches the character _ with index 9510 (5F16 or 1378) literally (case insensitive)
Match a single character present in the list below
[ \t]
* matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
matches the character with index 3210 (2016 or 408) literally (case insensitive)
\t matches a tab character (ASCII 9)
as
matches the characters as literally (case insensitive)
Match a single character present in the list below
[ \t]
* matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
matches the character with index 3210 (2016 or 408) literally (case insensitive)
\t matches a tab character (ASCII 9)
\$ matches the character $ with index 3610 (2416 or 448) literally (case insensitive)
2nd Capturing Group
(_*[A-Za-z]+[A-Za-z0-9_]*)
_
matches the character _ with index 9510 (5F16 or 1378) literally (case insensitive)
Match a single character present in the list below
[A-Za-z]
+ matches the previous token between one and unlimited times, as few times as possible, expanding as needed (lazy)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
Match a single character present in the list below
[A-Za-z0-9_]
* matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
_ matches the character _ with index 9510 (5F16 or 1378) literally (case insensitive)
Non-capturing group
(?:[ \t]=>[ \t]*\$(_*[A-Za-z]+[A-Za-z0-9_]*))?
? matches the previous token between zero and one times, as few times as possible, expanding as needed (lazy)
Match a single character present in the list below
[ \t]
matches the character with index 3210 (2016 or 408) literally (case insensitive)
\t matches a tab character (ASCII 9)
=>
matches the characters => literally (case insensitive)
Match a single character present in the list below
[ \t]
* matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
matches the character with index 3210 (2016 or 408) literally (case insensitive)
\t matches a tab character (ASCII 9)
\$ matches the character $ with index 3610 (2416 or 448) literally (case insensitive)
3rd Capturing Group
(_*[A-Za-z]+[A-Za-z0-9_]*)
Match a single character present in the list below
[ \t]
* matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
matches the character with index 3210 (2016 or 408) literally (case insensitive)
\t matches a tab character (ASCII 9)
} matches the character } with index 12510 (7D16 or 1758) literally (case insensitive)
4th Capturing Group
(.*)
.
matches any character
* matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
{ matches the character { with index 12310 (7B16 or 1738) literally (case insensitive)
Match a single character present in the list below
[ \t]
\/ matches the character / with index 4710 (2F16 or 578) literally (case insensitive)
foreach
matches the characters foreach literally (case insensitive)
Match a single character present in the list below
[ \t]
} matches the character } with index 12510 (7D16 or 1758) literally (case insensitive)
Global pattern flags
U modifier: Ungreedy. The match becomes lazy by default. Now a ? following a quantifier makes it greedy
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
s modifier: single line. Dot matches newline characters
Your regular expression does not match the subject string.Try launching the debugger to find out why.

Regular Expression
No Match

/
/
Uis

Test String