Quote:
Originally Posted by cooltechie
Hi,
I have strings in the format given below: I need to match -> one pair of Line String i.e from the Example below:
MULTILINESTRING ((175.641715 -40.860575, 175.641908 -40.860854), (185.641715 -45.860575, 182.641908 -35.860854))
Note: A multi line string can have any number of line strings.
I need to match:
Match 1 : 175.641715 -40.860575, 175.641908 -40.860854
Match 2 :185.641715 -45.860575, 182.641908 -35.860854
and so on...
Thanks,
Any help is greatly appreciated.
|
There are a few details you don't explain clearly, but I have assumed that your data is line orientated with each line being composed of many pairs of pairs of (co-ordinates?) surrounded by brackets. Here is a perl implementation of that:
Code:
`#!/usr/bin/perl
use strict;
use warnings;
use 5.10.0; # for "say"
my $string = "((175.641715 -40.860575, 175.641908 -40.860854), (185.641715 -45.860575, 182.641908 -35.860854))\n"
. "((175.641715 -40.860575, 175.641908 -40.860854), (185.641715 -45.860575, 182.641908 -35.860854))";
my $pair = qr{[-]?\d+\.\d+ [-]?\d+\.\d+}; # match a pair of (co-ordinates on cartesian plane)?
my $dbl_pair = qr{$pair, $pair}; # match two pairs sepearted by a space
my @matches = ($string =~ m{\(($dbl_pair)\)}gso); # match any number of pairs surrounded by brackets and assign all to matches
map {say} @matches; # map the function say over each element of matches, effectively printing
# them out one per line
For the string in the file (the one in your question, repeated twice with a newline between the repetitions) this gives
Code:
$ perl mst.pl
175.641715 -40.860575, 175.641908 -40.860854
185.641715 -45.860575, 182.641908 -35.860854
175.641715 -40.860575, 175.641908 -40.860854
185.641715 -45.860575, 182.641908 -35.860854
Which seems to meet the requirement.