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
`
\[((?P<serial>\d{14}){0,1}[\.\w]*-(?P<pccode>[\dA-Z]{8}))\] +
`
g
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
1st Capturing Group
((?P<serial>\d{14}){0,1}[\.\w]*-(?P<pccode>[\dA-Z]{8}))
Named Capture Group serial
(?P<serial>\d{14}){0,1}
{0,1} matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
\d
matches a digit (equivalent to [0-9])
{14} matches the previous token exactly 14 times
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)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
\w matches any word character (equivalent to [a-zA-Z0-9_])
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
Named Capture Group pccode
(?P<pccode>[\dA-Z]{8})
Match a single character present in the list below
[\dA-Z]
{8} matches the previous token exactly 8 times
\d matches a digit (equivalent to [0-9])
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
\] matches the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
matches the character with index 3210 (2016 or 408) literally (case sensitive)
Global pattern flags
g modifier: global. All matches (don't return after first match)
Your regular expression does not match the subject string.

Regular Expression
No Match

`
`
g

Test String