Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by Anonymous Monk
on Oct 17, 2000 at 07:40 UTC ( [id://37089]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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) ?

for example:

qwert(ui)" should return ( qwerty"(ff) should return " qwer)()(" should return )
and so on.

THX

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How to find which comes first: '"', '(', or ')'?
by merlyn (Sage) on Oct 17, 2000 at 07:51 UTC
    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>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found