Regex question

B

Boerni

Hi

I have a string which looks like this

text text %%slotname%% text text %%slotname%%...

What I would like to do, is to get the values between the '%%' signs. The
Problem is, I dont know how many of those %%slotname%% occur in the string.

Is there any way to solve this problem with a regex?

what i've tried is something like:
($slot1, $slot2, $slot3, $slot4, $slot5) = $a_msg =~
/.*?%%(.*?)%%.*?%%(.*?)%%.*?%%(.*?)%%.*?%%(.*?)%%.*?%%(.*?)%%/;

(assuming there are no more than 5 slots in a string)... the Problem is,
this only works if there are exactly 5 slots.

What i'd like is something like:

(@slots) = $a_msg =~ /some regex/

Is there any way to do this?

Thanks in advance
Bernard
 
J

Jürgen Exner

Boerni said:
I have a string which looks like this

text text %%slotname%% text text %%slotname%%...

What I would like to do, is to get the values between the '%%' signs.
The Problem is, I dont know how many of those %%slotname%% occur in
the string.
Is there any way to solve this problem with a regex?

Why regexp?
I would just split() the string at '%%' and then every other element
contains the values you are looking for.

jue
 
D

DJ Stunks

Boerni said:
Hi

I have a string which looks like this

text text %%slotname%% text text %%slotname%%...

What I would like to do, is to get the values between the '%%' signs. The
Problem is, I dont know how many of those %%slotname%% occur in the string.

Is there any way to solve this problem with a regex?

what i've tried is something like:
($slot1, $slot2, $slot3, $slot4, $slot5) = $a_msg =~
/.*?%%(.*?)%%.*?%%(.*?)%%.*?%%(.*?)%%.*?%%(.*?)%%.*?%%(.*?)%%/;

(assuming there are no more than 5 slots in a string)... the Problem is,
this only works if there are exactly 5 slots.

What i'd like is something like:

(@slots) = $a_msg =~ /some regex/

use the /g regular expression modifier.

-jp
 
J

jl_post

Boerni said:
I have a string which looks like this

text text %%slotname%% text text %%slotname%%...

What I would like to do, is to get the values between the '%%' signs. The
Problem is, I dont know how many of those %%slotname%% occur in the string.

Is there any way to solve this problem with a regex?

What i'd like is something like:

(@slots) = $a_msg =~ /some regex/


You're very close, Bernard. You just need to learn about the "/g"
option used with regular expressions. If you read "perldoc perlop" in
the "m/PATTERN/cgimosx" section, you'll see this text:

The "/g" modifier specifies global pattern matching--that is,
matching as many times as possible within the string. ...
In list context, it returns a list of the substrings matched
by any capturing parentheses in the regular expression.

Therefore, write your code with the "/g" option, like:

@slots = $a_msg =~ m/%%(.*?)%%/g;

and all the text found between "%%" will be placed as an element in the
@slots array.

I hope this helps, Bernard.

-- Jean-Luc
 
B

Boerni

I would just split() the string at '%%' and then every other element
contains the values you are looking for.

yep...thats how I solved it in the meantime.. but somehow i wondered if it
could be done with a regex.

Thank you very much anyway
 
D

DJ Stunks

Mirco said:
Thus spoke Boerni (on 2006-10-31 16:31):
I have a string which looks like this
text text %%slotname%% text text %%slotname%%...

What i'd like is something like:
(@slots) = $a_msg =~ /some regex/
Is there any way to do this?

Yes, but you have to 'quote* the %%
properly, otherwise it gets expanded
on the way to the match, like:

...
my $text = 'text text %%slotname%% text text %%slotname%% text';
my $rg = '\%\%([^\%]+)\%\%';

????

maybe you should start trying some of this stuff before posting?

-jp
 
T

Tad McClellan

Boerni said:
I have a string which looks like this

text text %%slotname%% text text %%slotname%%...

What I would like to do, is to get the values between the '%%' signs. The
Problem is, I dont know how many of those %%slotname%% occur in the string.

Is there any way to solve this problem with a regex?


Use m//g in a list context:

my @matches = $a_msg =~ /%%(.*?)%%/g;
 
B

Boerni

Thanks alot for your help everyone... i'm going to read up on the /g
modifier

Bernard
 
X

Xicheng Jia

Abigail said:
Mirco Wahab ([email protected]) wrote on MMMMDCCCIX September
MCMXCIII in <URL:$$ Thus spoke Abigail (on 2006-10-31 20:52):
$$
$$ > Untested:
$$ >
$$ > @slots = $a_msg =~ /%%([^%]*(?:%[^%]+)*)%%/g;
$$ >
$$
$$ If we know /exactly/ what the boundaries are
$$ happen to be (%%searched%%), why wouldn't a
$$ simple:
$$
$$ my @slots = $a_msg =~ /%%(.+?)%%/g;
$$
$$ do (even on '%%some%wiered%stuff%%')?

Two reasons.
My solution doesn't use any backtracking,

No, this is not about "backtracking". check a string that doesnot match
your pattern, it will use backtracking anyway. In Perl, unless you use
(?>...) construct, your pattern will always try to save some status
information and then backtrack it later. :)

A similar comparion might be between "[^"]*" and ".*?", which is all
about how different quantifiers work instead of whether backtracking is
involved.

Regards,
Xicheng
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top