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
`
(?m)^NAME="(?P<name>(?P<disk>[a-z]+)(?P<partition>\d*))"\sFSTYPE="(?P<fstype>\w*)"\sMOUNTPOINT="(?P<mountpoint>[\w\[\]/]*)"\sSIZE="(?P<size>\d*)"\sTYPE="(?P<type>\w+)"$
`
gm
(?m)
match the remainder of the pattern with the following effective flags: gm
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
^ asserts position at start of a line
NAME="
matches the characters NAME=" literally (case sensitive)
Named Capture Group name
(?P<name>(?P<disk>[a-z]+)(?P<partition>\d*))
Named Capture Group disk
(?P<disk>[a-z]+)
Match a single character present in the list below
[a-z]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive)
Named Capture Group partition
(?P<partition>\d*)
\d
matches a digit (equivalent to [0-9])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
" matches the character " with index 3410 (2216 or 428) literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
FSTYPE="
matches the characters FSTYPE=" literally (case sensitive)
Named Capture Group fstype
(?P<fstype>\w*)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
" matches the character " with index 3410 (2216 or 428) literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
MOUNTPOINT="
matches the characters MOUNTPOINT=" literally (case sensitive)
Named Capture Group mountpoint
(?P<mountpoint>[\w\[\]/]*)
Match a single character present in the list below
[\w\[\]/]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\w matches any word character (equivalent to [a-zA-Z0-9_])
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
\] matches the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
" matches the character " with index 3410 (2216 or 428) literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
SIZE="
matches the characters SIZE=" literally (case sensitive)
Named Capture Group size
(?P<size>\d*)
\d
matches a digit (equivalent to [0-9])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
" matches the character " with index 3410 (2216 or 428) literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
TYPE="
matches the characters TYPE=" literally (case sensitive)
Named Capture Group type
(?P<type>\w+)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
" matches the character " with index 3410 (2216 or 428) literally (case sensitive)
$ asserts position at the end of a line
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.

Regular Expression
No Match

`
`
gm

Test String