Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: difference in regex

by haj (Vicar)
on May 29, 2018 at 13:58 UTC ( [id://1215371]=note: print w/replies, xml ) Need Help??


in reply to difference in regex

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:
my ($val) = ($row =~ /(.*),[^,]*$/);

A good reference for all this, and a lot more, is perlretut.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1215371]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-26 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found