我试图熟悉RegEx,特别是JavaScript RegExp。这是为了我的学习,我意识到字符串函数存在来解决给定的问题。
在一个不确定长度的字符串中,我试图选择所有找到的字符,包括起始和结束字符。例如:
输入:
LINESTRING(-83.1732840000000000 40.0046110000000000, -83.1733380000000000 40.0048360000000000)LINESTRING(-83.1733380000000000 40.0048360000000000,-83.17219100000000000000 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.172191000000000000 40.0049080000000000)( - 83.1733380000000000 40.0048360000000000,-83.172191000000000000 40.0049080000000000)( - 83.1733380000000000 40.0048360000000000,-83.17219100000000000000 40.0049080000000000)
我对正则表达式的有限了解让我相信以下内容应该有效:
patNew = new RegExp("(\(.*\)(?=[LINESTRING]))", "g");
但它会产生以下输出:
[“LINESTRING(-83.1732840000000000 40.0046110000000000, -83.1733380000000000 40.0048360000000000)LINESTRING(-83.1733380000000000 40.0048360000000000,-83.17219100000000000000 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”的第2和第3个实例;为什么?
我知道没有选择最后一个段,因为对于它,一个右括号后面没有“LINESTRING”。这是正确的解释吗?
如何选择b / w()中的所有内容并将LINESTRING留在不确定长度的字符串中?另外,我如何解释最后一部分?
感谢。
答案 0 :(得分:1)
你需要使用反斜杠转义regexes中的括号,如果你从它们创建正则表达式,还需要字符串文字中的反斜杠:
var patNew = /(\(.*\)(?=[LINESTRING]))/g;
// or
var patNew = new RegExp("(\\(.*\\)(?=[LINESTRING]))", "g");
我强烈推荐正则表达式文字,它更具可读性。
顺便说一下,没有理由围绕整个正则表达式创建一个捕获组,你肯定不希望[LINESTRING]
成为character class而是一个文字字符串 - 你需要同时转义括号。你需要制作repetition non-greedy,否则你将从第一个开头到倒数第二个右括号进行匹配。此外,通过该预测,您将无法获得最后一个组 - 它不再是LINESTRING
,因此要么允许string ends ($
)作为替代,要么放弃它。
var patNew = /\(.*?\)(?=LINESTRING|$)/g;
但是,对于您的实际问题,匹配/\([^)]*\)/g
应该足够了。
答案 1 :(得分:0)
您正在错误地创建正则表达式:
patNew = new RegExp("(\\(.*\\)(?=[LINESTRING]))", "g");
你需要加倍反斜杠。
或者,使用本机正则表达式语法:
patNew = /(\(.*\)(?=[LINESTRING]))/g;
答案 2 :(得分:0)
如果您的要求实际上只是(
和)
之间的所有内容,则此简单模式应该/\(.*?\)/
LINESTRING在这里代表什么?是否与解析相关?