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 (3)
`
(?P<name>[A--Ÿ\s-\.\/\(\)'\/]+)(?:\s)*(?P<dosage>(?:[\d\s]+(?:ml|ML|mg|MG|g|%)*[\s/]*)*)(?:\s)(?P<adminmode2>[A--Ÿ\s-\.\/\(\)'\/]*)(?P<separator>,|\.\s\/)(?:\s)(?P<adminmode1>[a-zA--ÿ\s\(\)\-'\.0-9]+)
`
gm
Named Capture Group name
(?P<name>[A--Ÿ\s-\.\/\(\)'\/]+)
Match a single character present in the list below
[A--Ÿ\s-\.\/\(\)'\/]
+ 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 65) and Z (index 90) (case sensitive)
À-Ÿ matches a single character in the range between À (index 192) and Ÿ (index 376) (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
This hyphen is treated literally, which might be confusing for others. Consider escaping it or placing at the start or end of the class!
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\( matches the character ( with index 4010 (2816 or 508) literally (case sensitive)
\) matches the character ) with index 4110 (2916 or 518) literally (case sensitive)
' matches the character ' with index 3910 (2716 or 478) literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
Non-capturing group
(?:\s)*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Named Capture Group dosage
(?P<dosage>(?:[\d\s]+(?:ml|ML|mg|MG|g|%)*[\s/]*)*)
Non-capturing group
(?:[\d\s]+(?:ml|ML|mg|MG|g|%)*[\s/]*)*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below
[\d\s]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equivalent to [0-9])
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Non-capturing group
(?:ml|ML|mg|MG|g|%)*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
1st Alternative
ml
ml
matches the characters ml literally (case sensitive)
2nd Alternative
ML
ML
matches the characters ML literally (case sensitive)
3rd Alternative
mg
mg
matches the characters mg literally (case sensitive)
4th Alternative
MG
MG
matches the characters MG literally (case sensitive)
5th Alternative
g
g matches the character g with index 10310 (6716 or 1478) literally (case sensitive)
6th Alternative
%
% matches the character % with index 3710 (2516 or 458) literally (case sensitive)
Match a single character present in the list below
[\s/]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
Non-capturing group
(?:\s)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Named Capture Group adminmode2
(?P<adminmode2>[A--Ÿ\s-\.\/\(\)'\/]*)
Match a single character present in the list below
[A--Ÿ\s-\.\/\(\)'\/]
* matches the previous token between zero 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 65) and Z (index 90) (case sensitive)
À-Ÿ matches a single character in the range between À (index 192) and Ÿ (index 376) (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
This hyphen is treated literally, which might be confusing for others. Consider escaping it or placing at the start or end of the class!
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\( matches the character ( with index 4010 (2816 or 508) literally (case sensitive)
\) matches the character ) with index 4110 (2916 or 518) literally (case sensitive)
' matches the character ' with index 3910 (2716 or 478) literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
Named Capture Group separator
(?P<separator>,|\.\s\/)
1st Alternative
,
, matches the character , with index 4410 (2C16 or 548) literally (case sensitive)
2nd Alternative
\.\s\/
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
Non-capturing group
(?:\s)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Named Capture Group adminmode1
(?P<adminmode1>[a-zA--ÿ\s\(\)\-'\.0-9]+)
Match a single character present in the list below
[a-zA--ÿ\s\(\)\-'\.0-9]
+ 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)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
À-ÿ matches a single character in the range between À (index 192) and ÿ (index 255) (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
\( matches the character ( with index 4010 (2816 or 508) literally (case sensitive)
\) matches the character ) with index 4110 (2916 or 518) literally (case sensitive)
\- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
' matches the character ' with index 3910 (2716 or 478) literally (case sensitive)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
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