Avatar of trevor1940
trevor1940
 asked on

perl: Spliting strings into parts with REGEX

Given the code snippet bellow  I need to extract each Place Name & Coord string

There might be zero 1 or many however I can't be sure what the delimiter will be when there are multiple the two samples are either  a comer or semi colon
I do know
 Place Name (Coord string) are in this format

print "Hello World!\n";
my @examples=("Place Name (Coord string )","Place Name2 (Coord string 2);Place Name3 (Coord string 3);Place Name4 (Coord string 4)","Place Name5 (Coord string 5), Place Name5 (Coord string 5),Place Name6 (Coord string 6)","Not Wanted");

foreach my $example (@examples){
    if($example =~ m/(.*)\((.*)\)([,;])?/g){
        
        my $Place =$1;
        my $Coord = $2;
        
        # do stuff with each
        print "$Place  $Coord\n";
    }
    
}

Open in new window



I thought about splitting on the delimiter then pushing into an array which might work for multiple but seems to fail for single
Regular ExpressionsPerl

Avatar of undefined
Last Comment
trevor1940

8/22/2022 - Mon
Terry Woods

What if you use this as a pattern?
[^;,]+

Open in new window


That would match groups of characters excluding the delimiter.
ASKER CERTIFIED SOLUTION
Dave Cross

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
noci

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
trevor1940

ASKER
Thanx

Both Solutions work
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy