Search code examples
javamysqlregexwkt

Using a regular expression to convert WKT to GeoJSON


I obtain data as WKT from mysql:

  POLYGON((148.798828125 -34.37971258046219,
           148.86474609375 -34.10270799317487,
           149.23828125 -34.28899186503752,
           149.2108154296875 -34.601563177240884,
           148.853759765625 -34.51560953848203,
           148.5186767578125 -34.68291096793205,
           148.38134765625 -34.542762387234845,
           148.798828125 -34.37971258046219))

I use this regex to get all the paths within the polygon:

   (\((-?\d+.\d+\s?-?\d+.\d+,?)+\))*

However I find that there is a large number of empty matches in this,how can I tighten this regex for better matches?


Solution

  • You could avoid the empty matches by changing the last * (0 or more) to a + (1 or more):

    (\((-?\d+.\d+\s?-?\d+.\d+,?)+\))+