Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
PCRE2 (PHP >=7.3)

/
^(?P<str_lit>(?P<begin_quote>(?P<single_quote>')|\")(?:(?(single_quote)\"|')|\\[\"']|\\[^\r\n ]|[^\"'\\\r\n])*?(?P=begin_quote))$
/
gm

Description

This regex matches a single one-line C-like string literal. The parsing rules are almost exactly the same as C string literals:

  • A string literal can be surrounded by either single or double quotes, but they must match.
  • In a double quoted string, single quotes can optionally not be preceded by a backslash. And vice versa.
  • In a double quoted string, double quotes must be escaped. And vice versa.
  • Backslashes themselves must be escaped.
  • Any character but a newline, line-feed, or space can follow a backslash, so this regex can be used with any programming language.

The group <begin_quote> stores the quote character that the string literal is surrounded with.

Submitted by A-Paint-Brush - 4 months ago