Solved

# Pattern matching Help

Posted on 2011-10-18
131 Views
Hi,

I have strings in the format given below: I need to match -> one pair of Line String i.e from the Example below:

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

MULTILINESTRING ((175.641715 -40.860575, 175.641908 -40.860854), (185.641715 -45.860575, 182.641908 -35.860854))

Thanks,

Any help is greatly appreciated.

0
Question by:Techsavy

LVL 74

Expert Comment

You might get away with using:

``````(?<=\()[^)]+
``````

but that's if your language/text editor supports lookbehind. If not, then you can probably do:

``````\d+\.\d+ -?\d+\.\d+, \d+\.\d+ -?\d+\.\d+
``````
0

Author Comment

Hi Kaufmed,

Thank you. However, A single LineString can contain more than two coordinates. Your second option matches only two coordiantes per LineString correct?
0

LVL 74

Expert Comment

I imagined you would run the match code repeatedly based on your separation of "Match 1" and "Match 2". Can you provide an example of the the exact input and exact output?
0

Author Comment

Also,  for matching a single point,

(-?\d+\.?\d*) may be is the most apt one than \d+\.\d+
0

LVL 74

Expert Comment

I'm not sure what you mean by "most apt," but yes, either should work.
0

Author Comment

Input:  MULTILINESTRING ((175.641715 -40.860575, 175.641908 -40.860854), (185.641715 -45.860575, 182.641908 -35.860854))

Output matches:  set of points inside each Parenthesis.

so basically my regular expression should pull out:

Match 1  :  175.641715 -40.860575, 175.641908 -40.860854

Match 2:  185.641715 -45.860575, 182.641908 -35.860854

and if there is a third pair of parenthesis then I would have a 3rd pair of linestring.

and note it is not necessary that a parenthesis can contain only two points. it can contain any number of points.

Let me know if this is not clear

0

LVL 74

Expert Comment

Which language/text editor are you using?
0

Author Comment

I am actually trying to match this in a XML output.
0

Author Comment

Hi,

0

Author Comment

Ok I think ,  i need to simplify this to get a regular expression I am looking for :

ABC ((175.66286 -40.896472, 175.663064 -40.898479, 175.663136 -40.898845, 175.663274 -40.899374, 175.663373 -40.899702))

How do I get everything between the second pair of parentheses?  also note each comma seperated value is a pair of latitude and longitude.

0

Author Comment

so we have to a have a subgroup that matches  lat/long also.

0

LVL 9

Accepted Solution

Okay, so I know you said you're looking for this in an XML output, but I refer you back to kaufmed's question: Which flavor of regex are you using? Because that is kind of important to know, to get an actually useful answer...but just because it's what I know best, let me use PCRE w/a PHP function:
``````preg_match_all("/(-?\d+\.?\d*) (-?\d+\.?\d*)/", \$matches);
``````
So, it turned out a bit simpler than I expected, but I ran it and it found all 4 match pairs, as well as the subgrouped lat/long pairs(all located inside the \$matches array):
``````Match 1 of regular expression #1: 175.66286 -40.896472
Match 2 of regular expression #1: 175.663064 -40.898479
Match 3 of regular expression #1: 175.663136 -40.898845
Match 4 of regular expression #1: 175.663274 -40.899374
Match 5 of regular expression #1: 175.663373 -40.899702
Match 1 of parenthesized group 1: 175.66286
Match 2 of parenthesized group 1: 175.663064
Match 3 of parenthesized group 1: 175.663136
Match 4 of parenthesized group 1: 175.663274
Match 5 of parenthesized group 1: 175.663373
Match 1 of parenthesized group 2: -40.896472
Match 2 of parenthesized group 2: -40.898479
Match 3 of parenthesized group 2: -40.898845
Match 4 of parenthesized group 2: -40.899374
Match 5 of parenthesized group 2: -40.899702
``````

The perl R.E. equivalent would be
/(-?\d+\.?\d*) (-?\d+\.?\d*)/g
but you would still need some additional perl code to capture the matches...
0

## Featured Post

### Suggested Solutions

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…