用于匹配SQL空间数据的正则表达式

时间:2014-06-10 06:30:05

标签: sql regex spatial

如何创建匹配MySQL空间多线字符串坐标的正则表达式。 匹配:

(33 44, 98 33)
(10 10, 20 20), (15 15, 30 15)
(10 10, 20 20), (15 15, 30 15), (10 10, 11 11, 22 22)

1 个答案:

答案 0 :(得分:2)

匹配每个坐标:

(\((?:\d+\s\d+,\s)*\d+\s\d+\))

Regular expression visualization

/(
  \(         # opening parenthesis  "("
    (?:      # non-capturing group  "("
      \d+    # one or more number   "(33"
      \s     # a space              "(33 "
      \d+    # one or more number   "(33 44"
      ,\s    # a comma and a space  "(33 44, "
    )*       # repeat n time        "(33 44, "
    \d+\s\d+ # plus a last one      "(33 44, 98 33"
  \)         # closing parenthesis  "(33 44, 98 33)"
)/x

匹配每个坐标组(=每一行):

((?:\((?:\d+\s\d+,\s)*\d+\s\d+\),\s)*\((?:\d+\s\d+,\s)*\d+\s\d+\))

Regular expression visualization

/(
  (?:                            # non-capturing group ""
    \((?:\d+\s\d+,\s)*\d+\s\d+\) # cf first regex      "(10 10, 20 20)"
    ,\s                          # comma and space     "(10 10, 20 20), "
  )*                             # repeat n time       "(10 10, 20 20), (15 15, 30 15), "
  \((?:\d+\s\d+,\s)*\d+\s\d+\)   # plus a last one     "(10 10, 20 20), (15 15, 30 15), (10 10, 11 11, 22 22)"
)/x