Contributed by Anonymous Monk
on Oct 17, 2000 at 07:40 UTC
I have a string, and I want to find which one of the following 3 characters come first (ie, is on the left-most side of a given string) ?
qwert(ui)" should return (
qwerty"(ff) should return "
qwer)()(" should return )
and so on.
|Answer: How to find which comes first: '"', '(', or ')'?|
contributed by merlyn
my ($first) = $string =~ /(["()])/;
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'...).
So, by parenthesizing the character class containing the
characters you want to find, the regex will return that
character when matched.
if ( ($F1, $F2, $Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/) )
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.
Please (register and) log in if you wish to add an answer
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
Outside of code tags, you may need to use entities for some characters:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||