in reply to Re^2: Need help using regex to extract multiple matches
in thread Need help using regex to extract multiple matches
I'm assuming you're referring to the regex in GrandFather's reply:
/data-src-hq="([^"]+)"/g
First, let me draw your attention to YAPE::Regex::Explain, which can explain regexes that do not have regex operators or features added after Perl verion 5.6:
There are also on-line regex explainers.c:\@Work\Perl\monks>perl use strict; use warnings; use YAPE::Regex::Explain; print YAPE::Regex::Explain->new(qr/data-src-hq="([^"]+)"/)->explain; __END__ The regular expression: (?-imsx:data-src-hq="([^"]+)") matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- data-src-hq=" 'data-src-hq="' ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- [^"]+ any character except: '"' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
Now let me address your narration.
img src=" is the first part to matchOk.
[^"] match everything but a quoteI would word this as match a single character from the class of all characters except a " (double-quote). It's important to realize that the [...] regex operator defines a character class or set (see Character Classes and other Special Escapes in perlre and also this topic in perlretut, perlrequick and perlrecharclass), and that all by itself, any [...] matches only a single character.
+" stop when you hit a quoteI would quarrel with this description. The + quantifier (see Quantifiers in perlre; see also the topic of quantifiers in perlretut and perlrequick) is associated with the expression before it, i.e., [^"]+ and I would read it as match one or more characters from the class/set of all characters except a double-quote. Again, the double-quote is not directly associated with the + quantifier in your +" — but see below because they
() return only what matches within the bracketsOk.
Am also curious what's the difference between +" and +?" since both seem to work
Again, note that the + or +? quantifiers affect the preceding [^"] character class, not the double-quote that follows. In the /data-src-hq="([^"]+)"/g match regex, the final " (double-quote) is not absolutely needed because [^"]+ will match as much as possible until it either hits a " or the end of the string. (I would still tend to use it because I like the feeling of security that well-defined boundaries give me. Also, a final " in the match will prevent a match with a "runaway" quote in a string in which the closing " is missing.) However, if you use a [^"]+? "lazy" or "non-greedy" expression instead, the final " becomes vital to matching the entire contents of the double-quoted substring. Try this:
A lazy quantifier matches the minimum necessary for an overall match. A final " in the regex is necessary in this case to capture the entire quoted substring. Take a look at this and be sure you understand what's going on, i.e., the difference between lazy and greedy matching.c:\@Work\Perl\monks>perl use strict; use warnings; my $s = 'foo "xyzzy" bar'; print qq{+? (lazy) quantifier with final ": matched '$1' \n} if $s +=~ /"([^"]+?)"/; print qq{+? (lazy) quantifier without final ": matched '$1' \n} if $s +=~ /"([^"]+?)/; __END__ +? (lazy) quantifier with final ": matched 'xyzzy' +? (lazy) quantifier without final ": matched 'x'
Give a man a fish: <%-{-{-{-<
|
|---|