Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning Regular Expressions
This is the forum to discuss the Wrox book Beginning Regular Expressions by Andrew Watt; ISBN: 9780764574894
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Regular Expressions section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 19th, 2011, 01:48 PM
Authorized User
Points: 134, Level: 2
Points: 134, Level: 2 Points: 134, Level: 2 Points: 134, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default Pattern match help!

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.
  #2 (permalink)  
Old October 18th, 2012, 07:04 PM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Quote:
Originally Posted by cooltechie View Post
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.
__________________
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk


Similar Threads
Thread Thread Starter Forum Replies Last Post
State pattern versus Strategy Pattern disel2010 BOOK: Professional ASP.NET Design Patterns 2 March 15th, 2011 08:20 AM
Regex pattern to match carriage returns inside quotation marks? miikee55 Classic ASP Basics 4 September 21st, 2009 06:04 AM
Pattern extension? tdewey BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 1 May 13th, 2009 10:20 PM
template match doesnt match the required node Tomi XSLT 2 March 12th, 2007 06:24 AM
DirectoryInfo.GetFiles(pattern): search pattern fo arif_1947 VS.NET 2002/2003 1 October 19th, 2004 11:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.