一般来说,我正在努力适应 RegEx,尤其是 JavaScript RegExp。这是为了我的学习,我知道存在字符串函数来解决给定的问题。
在一个长度不定的字符串中,我试图选择所有在 b/wa 开始和结束字符中找到的字符。例如:
输入:
LINESTRING (-83.1732840000000000 40.0046110000000000, -83.1733380000000000 40.0048360000000000)LINESTRING (-83.1733380000000000 40.0048360000000000, -83.1721910000000000 40.0049080000000000)LINESTRING (-83.1733380000000000 40.0048360000000000, -83.1721910000000000 40.0049080000000000)LINESTRING (-83.1733380000000000 40.0048360000000000, -83.1721910000000000 40.0049080000000000)
所需输出:
(-83.1732840000000000 40.0046110000000000, -83.1733380000000000 40.0048360000000000) (-83.1733380000000000 40.0048360000000000, -83.1721910000000000 40.0049080000000000) (-83.1733380000000000 40.0048360000000000, -83.1721910000000000 40.0049080000000000) (-83.1733380000000000 40.0048360000000000, -83.1721910000000000 40.0049080000000000)
我对正则表达式的有限了解使我相信以下应该有效:
patNew = new RegExp("(\(.*\)(?=[LINESTRING]))", "g");
但它会产生以下输出:
["LINESTRING (-83.1732840000000000 40.0046110000000000, -83.1733380000000000 40.0048360000000000)LINESTRING (-83.1733380000000000 40.0048360000000000, -83.1721910000000000 40.0049080000000000)LINESTRING (-83.1733380000000000 40.0048360000000000, -83.1721910000000000 40.0049080000000000)LINESTRIN", "LINESTRING (-83.1732840000000000 40.0046110000000000, -83.1733380000000000 40.0048360000000000)LINESTRING (-83.1733380000000000 40.0048360000000000, -83.1721910000000000 40.0049080000000000)LINESTRING (-83.1733380000000000 40.0048360000000000, -83.1721910000000000 40.0049080000000000)LINESTRIN", "LINESTRING (-83.1732840000000000 40.0046110000000000, -83.1733380000000000 40.0048360000000000)LINESTRING (-83.1733380000000000 40.0048360000000000, -83.1721910000000000 40.0049080000000000)LINESTRING (-83.1733380000000000 40.0048360000000000, -83.1721910000000000 40.0049080000000000)LINESTRIN"]
我错过了什么?请帮忙解释一下。另外,我在 rubular.com 上尝试过,它接近我需要的。这是rubular的输出。
(-83.1732840000000000 40.0046110000000000, -83.1733380000000000 40.0048360000000000)LINESTRING (-83.1733380000000000 40.0048360000000000, -83.1721910000000000 40.0049080000000000)LINESTRING (-83.1733380000000000 40.0048360000000000, -83.1721910000000000 40.0049080000000000)
在 rubular 中,“LINESTRING”的第二个和第三个实例出现在选择中;为什么?
我知道没有选择最后一段,因为对于它,右大括号后面没有“LINESTRING”。这是正确的解释吗?
如何选择所有 b/w () 并在不定长度的字符串中省略 LINESTRING?另外,我将如何计算最后一部分?
谢谢。