Tanumoy
Tanumoy

Reputation: 3

Perl : Decoding Regex

I would highly appreciate if somebody could help me understand the following.

=~/(?<![\w.])($val)(?![\w.])/gi)

This what i picked up but i dont understand this.

Lookaround: (?=a) for a lookahead, ?! for negative lookahead, or ?<= and ?<! for lookbehinds (positive and negative, respectively).

Upvotes: 0

Views: 296

Answers (4)

optional
optional

Reputation: 2071

define $val then watch the regex engine work with rxrx - command-line REPL and wrapper for Regexp::Debugger

it shows output like this but in color

            Matched
            |
            VVV
/(?<![\w.])(dog)(?![\w.])/


                                            |
                                            V
'The quick brown fox jumps over the lazy dog'
                                         ^^^

[Visual of regex at 'rxrx' line 0]         [step: 189]

It also gives descriptions like this

(?<!              #    Match negative lookbehind
  [\w.]           #      Match any of the listed characters
)                 #    The end of negative lookbehind
(                 #    The start of a capturing block ($1)
  dog             #      Match a literal sequence ("dog")
)                 #    The end of $1
(?!               #    Match negative lookahead
  [\w.]           #      Match any of the listed characters
)                 #    The end of negative lookahead

Upvotes: 0

Bulrush
Bulrush

Reputation: 558

I use these sites to help with testing and learning and decoding regex:

https://regex101.com/: This one dissects and explains the expression the best IMO.

http://www.regexr.com/

Upvotes: 0

ikegami
ikegami

Reputation: 385546

Read (?<!PAT) as "not immediately preceded by text matching PAT".

Read (?!PAT) as "not immediately followed by text matching PAT".

Upvotes: 1

choroba
choroba

Reputation: 241758

The regex seems to search for $val (i.e. string that matches the contents of the variable $val) not surrounded by word characters or dots.

Putting $val into parentheses remembers the corresponding matched part in $1.

See perlre for details.

Note that =~ is not part of the regex, it's the "binding operator".

Similarly, gi) is part of something bigger. g means the matching happens globally, which has different effects based on the context the matching occurs in, and i makes the match case insensitive (which could only influence $val here). The whole expression was in parentheses, probably, but we can't see the opening one.

Upvotes: 2

Related Questions