?
Solved

Pattern matching Help

Posted on 2011-10-18
12
Medium Priority
?
136 Views
Last Modified: 2012-12-05
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
Comment
Question by:Techsavy
  • 7
  • 4
12 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36990095
You might get away with using:

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

Open in new window


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+

Open in new window

0
 

Author Comment

by:Techsavy
ID: 36990137
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 75

Expert Comment

by:käµfm³d 👽
ID: 36990150
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Techsavy
ID: 36990159
Also,  for matching a single point,

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

Expert Comment

by:käµfm³d 👽
ID: 36990167
I'm not sure what you mean by "most apt," but yes, either should work.
0
 

Author Comment

by:Techsavy
ID: 36990224
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 75

Expert Comment

by:käµfm³d 👽
ID: 36990266
Which language/text editor are you using?
0
 

Author Comment

by:Techsavy
ID: 36990586
I am actually trying to match this in a XML output.
0
 

Author Comment

by:Techsavy
ID: 36995073
Hi,

Can anyone please help on this regexpression?

0
 

Author Comment

by:Techsavy
ID: 36997526
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.

Please help!

0
 

Author Comment

by:Techsavy
ID: 36997528
so we have to a have a subgroup that matches  lat/long also.

0
 
LVL 9

Accepted Solution

by:
Derek Jensen earned 2000 total points
ID: 38535070
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);

Open in new window

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

Open in new window


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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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…
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…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question