Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Answer: How to find which comes first: '"', '(', or ')'?

( #37091=categorized answer: print w/ replies, xml ) Need Help??

Q&A > strings > How to find which comes first: ' " ', '(', or ')'? contributed by merlyn

my ($first) = $string =~ /(["()])/;
<Editor's Note>
From perlop:
If the `/g' option is not used, `m//' in list context returns a list consisting of the subexpressions matched by the parentheses in the pattern, i.e., (`$1', `$2', `$3'...).

Examples:
if ( ($F1, $F2, $Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/) )

So, by parenthesizing the character class containing the characters you want to find, the regex will return that character when matched.

The parentheses around the left-hand-side variable (here, $first) are required, to put the expression in list context. In scalar context, a regex will only return true or false.
</Editor's Note>

Comment on Answer: How to find which comes first: '&quot;', '(', or ')'?
Select or Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2014-09-21 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (167 votes), past polls