0

一般来说,我正在努力适应 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?另外,我将如何计算最后一部分?

谢谢。

4

3 回答 3

1

您确实需要使用反斜杠转义正则表达式中的括号,如果您从它们创建正则表达式,还需要字符串文字中的反斜杠:

var patNew = /(\(.*\)(?=[LINESTRING]))/g;
// or
var patNew = new RegExp("(\\(.*\\)(?=[LINESTRING]))", "g");

我强烈推荐正则表达式文字,它更具可读性。

顺便说一句,没有理由围绕整个正则表达式创建一个捕获组,而且您肯定不想[LINESTRING]成为字符类而是文字字符串 - 您还需要转义括号。并且您需要使重复 non-greedy,否则您将从第一个开头匹配到倒数第二个右括号。此外,通过该前瞻,您将不会得到最后一组 - 它不再跟随LINESTRING,因此要么允许字符串结束 ( $)作为替代方案,要么直接删除它。

var patNew = /\(.*?\)(?=LINESTRING|$)/g;

不过,对于您的实际问题,匹配/\([^)]*\)/g应该就足够了。

于 2013-05-01T14:51:09.903 回答
0

您正在错误地创建正则表达式:

patNew = new RegExp("(\\(.*\\)(?=[LINESTRING]))", "g");

您需要在反斜杠上加倍。

或者,使用本机正则表达式语法:

patNew = /(\(.*\)(?=[LINESTRING]))/g;
于 2013-05-01T14:47:50.247 回答
0

如果您的要求实际上只是介于两者之间的所有内容()那么这个简单的模式应该可以做到/\(.*?\)/

LINESTRING 在这里代表什么?它与解析有关吗?

于 2013-05-01T14:51:40.043 回答