regexp_pattern#
Return the pattern text and modifiers of a compiled regular expression.
Context-sensitive: list context gives you the raw pattern and the
modifier letters separately, which is usually what you want for
inspection and tests. Scalar context gives you the stringified
form Perl would produce if you interpolated the qr// into a
string — the (?^flags:pattern) rendering.
Like is_regexp, this function looks at the internal regex
pointer and is not fooled by blessing or overloading.
Synopsis#
use re 'regexp_pattern';
my ($pat, $mods) = regexp_pattern(qr/foo/i); # ('foo', 'i')
my $str = regexp_pattern(qr/foo/i); # '(?^i:foo)'
What you get back#
List context: a two-element list
($pattern, $modifiers).$patternis the original source text, without the(?^...:wrapper or trailing).$modifiersis a short string of modifier letters — the character-set prefix (l,u,a,aa, or empty for the default) followed by whichever ofm s i x xx n pare enabled.Scalar context on a regex: the full stringified form, e.g.
(?^i:foo).Scalar context on a non-regex:
&PL_sv_no— false, but defined. This lets idioms likeif (regexp_pattern($ref) eq '(?^i:foo)')run warning-free regardless of what$refholds.List context on a non-regex: the empty list.
Examples#
my ($p, $m) = regexp_pattern(qr/foo/); # ('foo', '')
my ($p, $m) = regexp_pattern(qr/foo/ix); # ('foo', 'ix')
my ($p, $m) = regexp_pattern(qr/(?u)bar/); # ('(?u)bar', 'u')
my $s = regexp_pattern(qr/foo/i); # '(?^i:foo)'
my @e = regexp_pattern("not a regex"); # () — empty list
my $f = regexp_pattern("not a regex"); # false but defined
Edge cases#
Called with zero or more than one argument — croaks with
Usage: re::regexp_pattern(sv).UTF-8 patterns keep their UTF-8 flag on the returned pattern.
The
dcharset (the default) produces no prefix letter.