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 Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding multiple matches between delimiters

Status
Not open for further replies.

schickeneder

Technical User
Oct 24, 2010
1
US
I am having trouble figuring out how to get perl to do what I want it to. I'm sure there's a simple solution, but after looking through forums and tutorials I can't seem to find it.

Given this text:
storeAddresses[!5793!] = {!street1! : !#333 Carr #14, Cotto Laurel!,

I want to extract elements between the '!'s, in other words I want to be able to parse the above string and get variables containing: 5793,street1,#333 Carr #14, Cotto Laurel

Here's what I've been working with but I don't understand perl expressions well enough to get it:

$_ = "storeAddresses[!5793!] = {!street1! : !#333 Carr #14, Cotto Laurel!,";
$_ =~ /!(.*?)!(.*?)!(.*)!(.*)!/;


Then they should be in $1,$2,$3 etc...

any tips?
 
You're RE could probably use a tune up, but if your incoming text is stable, the split() command may get you there faster.
Code:
 @arr = split(/\!/, "storeAddresses[!5793!] = {!street1! : !#333 Carr #14, Cotto Laurel!,");
Will result in the following split
Code:
$arr[0] = 'storeAddresses[';
$arr[1] = '5793' ;
$arr[2] = '] = {' ;
$arr[3] = 'street1' ;
$arr[4] = ' : ' ;
$arr[5] = '#333 Carr #14, Cotto Laurel' ;
$arr[6] = ',"';
From there you can work with elements 1, 3, and 5. Note that the pattern is all odd entries - so should there be additional !'s in your incoming data, they'll break out into the next 'odd' valued @arr entry.

In the split expresion, the leading \ in front of the ! (i.e. /\!/) is probably not necessary, but if you're new to perl's REs, that leading backslash is telling the parser to not put any special intrepretation on the character.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top