Page 1 of 1

how to set variable to results of regex in one line

Posted: Mon Jun 08, 2009 3:12 pm
by andersod2
Hi -- this is actually more of a perl question, but thought someone here might still know --

I want to be able to perform a regex match and set the results into a variable, but in one easy step rather than doing what I usually do, i.e.

if ( $matchstr=~ /(regex)/ ) {
$result = $1
}

I figure there is a better way than this right? I know I can do this with search/replace, but would like to preserve the original string.

...sorry if I'm missing something obvious....

Re: how to set variable to results of regex in one line

Posted: Mon Jun 08, 2009 5:40 pm
by akuji36

Re: how to set variable to results of regex in one line

Posted: Tue Jun 09, 2009 3:18 am
by prometheuzz
andersod2 wrote:Hi -- this is actually more of a perl question, but thought someone here might still know --

I want to be able to perform a regex match and set the results into a variable, but in one easy step rather than doing what I usually do, i.e.

if ( $matchstr=~ /(regex)/ ) {
$result = $1
}

I figure there is a better way than this right? I know I can do this with search/replace, but would like to preserve the original string.

...sorry if I'm missing something obvious....
AFAIK that is not possible and you will have to do it like you already mentioned in your post. But to be sure, I'd ask in a Perl forum if I were you: Perl has quite a few beat trick when it comes to regex and although I am familiar with PCRE, I'm far from a true Perl monk!

Good luck!

Re: how to set variable to results of regex in one line

Posted: Tue Jun 09, 2009 3:19 am
by prometheuzz
I don't see how that is going to help the original poster...