Problems? Is your data what you think it is? | |
PerlMonks |
Re: difference in regexby haj (Vicar) |
on May 29, 2018 at 13:58 UTC ( [id://1215371]=note: print w/replies, xml ) | Need Help?? |
A regular expression can tell you two things: Whether there's a match at all, and what some parts in the match are. You are using it in different ways, on two levels: $row =~ /.*,(.*)/ is a pattern match. It returns whether $row contains the pattern. If you have parentheses in the regex (and you have), then the part of the match within the parentheses is captured - and if you evaluate the pattern match in list context, these captures will be returned as a list. By writing my ($value) you create a list context, therefore you get whatever matched after the last comma. $row =~ s/,[^,]*$// is a substitution s/text/pattern/. Substitutions change the variable they operate upon, and they return the number of substitutions made, regardless of context. Hence the 1 in the second line: One substitution. You get the substring before the last comma in the variable $row by deleting the last comma and whatever follows it. If you want the second example to behave like the first, add a capture, and replace the substitution by a match, like this:
In Section
Seekers of Perl Wisdom
|
|