Javascript和Regex:获取捕获的字符串的索引 [英] Javascript and Regex: Get index of a captured string
问题描述
这是我的问题:
- 我有一个正则表达式,这个表达式包含一个,只有一个捕获组,
- 此正则表达式无法更改,
- 我有一个字符串,将与此正则表达式匹配,
- 正则表达式将匹配完整的字符串,它不是查找,如果正则表达式不能与字符串匹配,则函数将在到达此步骤之前失败。
- I have a regular expression, this expression contains one, and only one capture group,
- This regular expression cannot be changed,
- I have a string, that will be matched this regular expression,
- The regex will match the complete string, it's not a look-up, if the regex cannot be matched to the string, the function will fail prior reaching this step.
=>我想在字符串中获取捕获的子字符串位置,并且它的长度。
=> I want to get the captured sub-string position in the string, and it's length.
示例;
如果我的正则表达式是
^。*?\ / F?L?(\d +) $
我的字符串是
"( 413) 250/FL250"
我想得到 14
和 3
。
在这些情况下,搜索将返回 1
。
In those conditions, search would return 1
.
这是一个简单的例子,但我们可能会非常复杂正则表达式,但原则始终是相同的:一个且只有一个捕获组,并在主要字符串中找到捕获字符串的位置。
This is a simple example, but we can have extremely complex regex, however the principle is always the same: one and only one capture group, and find the position of the captured string in the main one.
非常感谢你的帮助,我被困了。
Thanks a lot for your help, I'm stucked.
EDITION:
所以我用ant做了些什么(我们的基础工作环境是ant),包括获取捕获组的leftContext,然后确定它的大小。为了获得leftContext,我只需在左侧部分移动捕获组的括号。例如:\\\(\ s)变为(\d)\。。
So I made something with ant (our base work environnement is ant) which consist of getting the leftContext of the capture group, then determine it's size. To get the leftContext, I simply move the parenthesis of the capture groupe at the left part. Ex: \d(\s) becomes (\d)\s.
所以我对此有疑问:
<macrodef name="Get_CaptureGroup_Position" >
<attribute name="text" />
<attribute name="mask" />
<attribute name="start" />
<attribute name="end" />
<sequential>
<var name="_GMLCS_modified_regex" unset="true"/>
<var name="_GMLCS_leftContext" unset="true"/>
<var name="_GMLCS_leftContext_len" unset="true"/>
<var name="_GMLCS_CapturedGroup" unset="true"/>
<var name="_GMLCS_CapturedGroup_len" unset="true"/>
<propertyregex property="_GMLCS_modified_regex" override="yes" input="@{mask}" regexp="(.*[^\\])\)([^?].*)" replace="\1\2" />
<propertyregex property="_GMLCS_modified_regex" override="yes" input="${_GMLCS_modified_regex}" regexp="(.*[^\\])\(([^?].*)" replace="\1)\2" />
<var name="_GMLCS_modified_regex" value="(${_GMLCS_modified_regex}" />
<propertyregex property="_GMLCS_leftContext" override="yes" input="@{text}" regexp="${_GMLCS_modified_regex}" select="\1" />
<propertyregex property="_GMLCS_CapturedGroup" override="yes" input="@{text}" regexp="@{mask}" select="\1" />
<getAttributeLength text="${_GMLCS_leftContext}" property="_GMLCS_leftContext_len" />
<getAttributeLength text="${_GMLCS_CapturedGroup}" property="_GMLCS_CapturedGroup_len" />
<math result="_GMLCS_leftContext_len" operation="+" operand1="${_GMLCS_leftContext_len}" operand2="1" />
<math result="_GMLCS_CapturedGroup_len" operation="+" operand1="${_GMLCS_leftContext_len}" operand2="${_GMLCS_CapturedGroup_len}" />
<var name="@{start}" value="${_GMLCS_leftContext_len}" />
<var name="@{end}" value="${_GMLCS_CapturedGroup_len}" />
<var name="_GMLCS_modified_regex" unset="true"/>
<var name="_GMLCS_leftContext" unset="true"/>
<var name="_GMLCS_leftContext_len" unset="true"/>
<var name="_GMLCS_CapturedGroup" unset="true"/>
<var name="_GMLCS_CapturedGroup_len" unset="true"/>
</sequential>
</macrodef>
我的问题是,当我通过这个正则表达式时:
My question is that, when I pass this regex:
(?:A|.*)/F?L?(\d+)\s*\d*(?:A|.*)
我得到:
第一个属性正则表达式:
First property regex:
(?:A|.*)/F?L?(\d+\s*\d*(?:A|.*) = CORRECT
第二个propoerty正则表达式:
Second propoerty regex:
(?:A|.*)/F?L?)\d+\s*\d*(?:A|.*) = CORRECT
Var:
((?:A|.*)/F?L?)\d+\s*\d*(?:A|.*) = CORRECT
开始和结束:7和10 =正确。
Start and End: 7 and 10 = CORRECT.
这实际上是正确的,但我相信它不应该是,我的问题是为什么(?:...)
块末尾的)没有删除?
This is actually correct, but I believe it should not be, my question is why the ")" at the end of (?:...)
blocks were not removed ?
推荐答案
这是我们对这个问题的最终答案。
这是由ANT完成的,但我认为它可以转换为javascript:
Here the final answer we have for our issue. It's done by ANT, but I think it is transposable to javascript:
<macrodef name="Get_CaptureGroup_Position" >
<attribute name="text" />
<attribute name="mask" />
<attribute name="start" />
<attribute name="end" />
<sequential>
<var name="_GMLCS_modified_regex" unset="true"/>
<var name="_GMLCS_leftContext" unset="true"/>
<var name="_GMLCS_leftContext_len" unset="true"/>
<var name="_GMLCS_CapturedGroup" unset="true"/>
<var name="_GMLCS_CapturedGroup_len" unset="true"/>
<propertyregex property="_GMLCS_modified_regex" override="yes" input="@{mask}" regexp="^((?:|(?:[^\\]|\\.)*))\(([^?].*)$" replace="(\1\2" />
<propertyregex property="_GMLCS_leftContext" override="yes" input="@{text}" regexp="${_GMLCS_modified_regex}" select="\1" />
<propertyregex property="_GMLCS_CapturedGroup" override="yes" input="@{text}" regexp="@{mask}" select="\1" />
<getAttributeLength text="${_GMLCS_leftContext}" property="_GMLCS_leftContext_len" />
<getAttributeLength text="${_GMLCS_CapturedGroup}" property="_GMLCS_CapturedGroup_len" />
<math result="@{start}" operation="-" operand1="${_GMLCS_leftContext_len}" operand2="${_GMLCS_CapturedGroup_len}" datatype="int"/>
<math result="@{start}" operation="+" operand1="${@{start}}" operand2="1" datatype="int"/>
<var name="@{end}" value="${_GMLCS_leftContext_len}" />
<var name="_GMLCS_modified_regex" unset="true"/>
<var name="_GMLCS_leftContext" unset="true"/>
<var name="_GMLCS_leftContext_len" unset="true"/>
<var name="_GMLCS_CapturedGroup" unset="true"/>
<var name="_GMLCS_CapturedGroup_len" unset="true"/>
</sequential>
这篇关于Javascript和Regex:获取捕获的字符串的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!