Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

meaning of print results of qr in perl

Tags:

regex

perl

From the documentation, I see

$rex = qr/my.STRING/is;
print $rex; # prints (?si-xm:my.STRING)

But I am not sure how to understand (?si-xm:...). If I do a print on qr/quick|lazy/, I got (?-xism:quick|lazy). What does it mean here (?-xism:...) too?

Thanks!

like image 616
Qiang Li Avatar asked Oct 22 '11 00:10

Qiang Li


People also ask

What does qr mean in Perl?

qr// is documented in perlop in the "Regexp Quote-Like Operators" section. Just like qq"..." aka "..." allows you to construct a string, qr/.../ allows you to construct a regular expression.

What is qr in regex?

The qr// is a regex quoting operator that stores my regex in a scalar (and as a quoting operator, its documentation shows up in perlop). The qr// compiles the pattern so it's ready to use when I interpolate $regex in the match operator.

What is =~ in Perl?

The Binding Operator, =~ Matching against $_ is merely the default; the binding operator ( =~ ) tells Perl to match the pattern on the right against the string on the left, instead of matching against $_ .

What is in Perl regex?

Regular Expression (Regex or Regexp or RE) in Perl is a special text string for describing a search pattern within a given text. Regex in Perl is linked to the host language and is not the same as in PHP, Python, etc. Sometimes it is termed as “Perl 5 Compatible Regular Expressions“.


1 Answers

As explained in the perlre man-page:

Any letters between ? and : act as flags modifiers [...]

The letters before the - are positive modifiers; those after it are negative modifiers. So, for example, (?-xism:quick|lazy) means that whitespace and comments are not allowed inside the parentheses, the parenthesized part is not case-sensitive, a dot . does not match newlines, and ^ and $ do not match line-start and line-end.

like image 124
ruakh Avatar answered Oct 05 '22 23:10

ruakh