Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Match a string with one of a list regex-URGENTLY!

Status
Not open for further replies.

KeziaKristina

Programmer
Nov 1, 2002
13
ID
Hi,
I need help to resolve my problem.I have tried resolved it in a week but I didn't get the solution.
The problem is like this:

I have a string like this (for example, it can be more complex):
$str = '$my_var = $your_var + 2;';

I want to split the string based on a list of regexp in an array like this:
%RE = qw(
1 \$[\w]+
2 [0-9]+
3 ;
4 =
5 \+
);

The first regex of that array should match the variabel name '$my_var' or '$your_var' (variabel name) from $str.
The second regex should match '2' (any number)
The third regex should match ';' (semicolon)
the fourth regex should match '=' (assign)
the fifth regex should match '+' (addition operator)

I want to get the keys from the array that match on the each element of $str.
So, from $str i get a string like this:

$key = '1 4 1 5 2 3'

Thanks
Hope get help immediately,

Kezia Kristina
 
Kezia,

You say that it "can be more complex". Fair enough, though it appears that you want to do more than simple matching.

Have a look at the Parse-Earley module from cpan at Mike

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Thanks Mike,
But I still don't understand working with object in Perl. I know just a little about Perl.
I tried to read your suggested link.
Is Parse::Earley not default object in Perl?
I tried to download it but it failed.

 
I've never used the module, sorry - I did a quick search on CPAN to find it.

What failure are you seeing? Mike

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
This isn't very general but it does work for your posted data.
Code:
my $key = $str;
$key =~ s/$RE{$_}/$_/g for 2, 1, 3, 4, 5;
The '2' has to go first or it replaces the other already-substituted numbers. Not very pretty.

jaa
 
Hi Mike,
I didn't get the module from the link that you suggested, but I get the module form And from that site I get a module that exactly right as I need. The Module is Parse::Lex

Thanks Mike
Thanks for jaa too..
God Bless U
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top