jasonline
jasonline

Reputation: 9056

Help with regular expression

In the following expression:

if (($$_ =~ /^.+:\s*\#\s*abcd\s+XYZ/)
  1. Where is $$_ taken from?
  2. The right side of the expression means to match one or more characters plus followed by colon, followed by zero or more spaces followed by # followed by one or more spaces folowed by 'abcd' followed by zero or more spaces followed by 'XYZ'?

Upvotes: 0

Views: 80

Answers (4)

Toto
Toto

Reputation: 91373

Have a look at this site

Here is the given explanation of your regex:

Token    Meaning

^        Matches beginning of input. If the multiline flag is set to true, 
         also matches immediately after a line break character.
.+       Matches any single character except newline characters.
         The + quantifier causes this item to be matched 1 or more times (greedy).
:        :
\s*      Matches a single white space character.
         The * quantifier causes this item to be matched 0 or more times (greedy).
\#       #
\s*      Matches a single white space character.
         The * quantifier causes this item to be matched 0 or more times (greedy).
abcd     abcd
\s+      Matches a single white space character.
         The + quantifier causes this item to be matched 1 or more times (greedy).
XYZ      XYZ

Upvotes: 0

jensgram
jensgram

Reputation: 31508

As for pt. 2:

Line beginning with (^) one or more characters (.+), colon (:), zero or more whitespace characters (\s*), a hash (\#), zero or more whitespace characters (\s*), the string "abcd" (abcd), one or more whitespace characters (\s+), then the string "XYZ" (XYZ).

(emphasis added on discrepancies.) Do note that there is no anchor on the end of line ($), thus this only concerns the beginning.

Upvotes: 1

ysth
ysth

Reputation: 98388

You have the last "one or more" and "zero or more" reversed from what the regex actually does.

$$_ dereferences the scalar reference in $_.

Upvotes: 2

canavanin
canavanin

Reputation: 2709

Concerning 2., your explanation of the regex is not entirely correct.

/^.+:\s*#\s*abcd\s+XYZ/

means one or more characters (starting at the beginning of the string) followed by a colon, followed by zero or more whitespace characters, followed by one hash character, followed by zero or more whitespace characters, followed by 'abcd', followed by one or more whitespace characters, followed by 'XYZ'.

Upvotes: 1

Related Questions