We help IT Professionals succeed at work.

perl: Spliting strings into parts with REGEX

trevor1940
trevor1940 asked
on
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
Comment
Watch Question

Terry WoodsIT Guru
Most Valuable Expert 2011

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

Open in new window


That would match groups of characters excluding the delimiter.
Perl programmer, author and trainer
Commented:
Splitting on the delimiter seems the simplest approach to me. I'm not sure why your attempt didn't work, but this does...

#!/usr/bin/perl

use strict;
use warnings;

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) {
  foreach my $split (split /[;,]/, $example) {
    while ($split =~ m/(.*)\((.*)\)/g) {

      my $Place = $1;
      my $Coord = $2;

      # do stuff with each
      print "$Place  $Coord\n";
    }
  }
}

Open in new window

nociSoftware Engineer
Distinguished Expert 2019
Commented:
Your code with a Slight difference.
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){
    foreach $ex (split(/[;,]/,$example)) {
       if($ex =~ m/([^(]*)\(([^)]*)\)/ ){
        
          my $Place =$1;
          my $Coord = $2;
        
        # do stuff with each
          print "$Place  $Coord\n";
      }
    }
}

Open in new window


Added a split for , & ;
and .* is hungry. So stop place in match of ( and stop coord in ).

Author

Commented:
Thanx

Both Solutions work