问题描述
我有字符串,我想在其中找到2个字:'开始'和'结束'。
I have strings, and i want to find in them 2 words: 'start' and 'end'.
'开始'和'结束'总是聚集在一起(也许我会在它们之间有另一个角色,但是如果我有'开始',我也会'结束'。
'start' and 'end' always come together (maybe i will have another characters between them, but if i have 'start', i will have 'end' too).
我尝试用regEx来源找到第一个'开始'而不是他自己的'结束',它会返回正确的子字符串。
I try to do with regEx source that find the first 'start' and than his own 'end', and it will return the correct substring.
字符串的例子: [我在这个例子中为每一对'开始'和'结束'写了为了清晰起见 (在真正的字符串中我不会有这个索引) - 答案总是在索引(1)之间
examples of strings: [i wrote in this examples index for every couple of 'start' and 'end' just for clarity (in the real strings i will not have this indexes)- the answer always between index (1)]
- 某事开始 something_needed 结束某事
//打印'something_needed'
- 开始(1)某事开始(2)某事结束(2)事情结束(1)开始结束
//打印'某事开始(2)结束(2)某事'
- 开始(1)某事开始(2)开始(3)结束(3)某事开始(4)结束(4)某事结束(2)某事结束(1)某事开始(5)某事结束(5)
//打印'某事开始**(2)开始(3)某事结束(3)某事开始(4)结束(4)某事结束(2)某事'
- something start something_needed end something
// print 'something_needed'
- start(1) something start(2) something end(2) something end(1) start something end
// print 'something start(2) something end(2) something'
- start(1) something start(2) start(3) something end(3) something start(4) end(4) something end(2) something end(1) something start(5) something end(5)
// print 'something start**(2) start(3) something end(3) something start(4) end(4) something end(2) something'
这是我在Javascript中的解决方案,但我更喜欢仅在regEx中的答案。
This is my solution in Javascript, but i prefer the answer in regEx only.
我发现所有开始,之后所有结束,而且 - 每个开始:count ++,每个结束:count--。当count == 0时,它是正确结束的位置。
i find all the start, and after that all the end, and than- for every start: count++, for every end: count--. when count == 0, it the position of the correct end.
function getStartEnd(str) {
str = " "+str+" ";
var start = matchPosArr(str, /[\d\s\r\n,\(\)\[\]\{\}]+START+(?=[\d\s\r\n,\(\)\[\]\{\}])/gi);
var end = matchPosArr(str, /[\d\s\r\n,\(\)\[\]\{\}]+END+(?=[\d\s\r\n,\(\)\[\]\{\}])/gi);
var count = 0; // counter
var si = 0; // index of start array
var ei = 0; // index of end array
var isStart = false;
while (true) {
if (ei >= end.length) {
alert('error');
break;
}
else if (si >= start.length) {
ei++;
count--;
if (count == 0) {
ei--;
}
}
else if (start[si] > end[ei]) {
ei++;
count--;
}
else if (start[si] < end[ei]) {
si++;
count++;
}
if (count == 0 && isStart==true) {
break;
}
isStart = true;
}
return str.substring(start[0]+("start ".length),end[ei]);
}
function matchPosArr(str, regEx) {
var pos = [];
while ((match = regEx.exec(str)) != null) {
pos.push(match.index);
}
return pos;
}
alert( getSelectFrom(str) );
推荐答案
以下是。
示例用法:
matchRecursiveRegExp("START text START text END text more END text", "START", "END");
// (c) 2007 Steven Levithan <stevenlevithan.com>
// MIT License
/*** matchRecursiveRegExp
Accepts a string to search, a left and right format delimiter
as regex patterns, and optional regex flags. Returns an array
of matches, allowing nested instances of left/right delimiters.
Use the "g" flag to return all matches, otherwise only the
first is returned. Be careful to ensure that the left and
right format delimiters produce mutually exclusive matches.
Backreferences are not supported within the right delimiter
due to how it is internally combined with the left delimiter.
When matching strings whose format delimiters are unbalanced
to the left or right, the output is intentionally as a
conventional regex library with recursion support would
produce, e.g. "<<x>" and "<x>>" both produce ["x"] when using
"<" and ">" as the delimiters (both strings contain a single,
balanced instance of "<x>").
examples:
matchRecursiveRegExp("test", "\\(", "\\)")
returns: []
matchRecursiveRegExp("<t<<e>><s>>t<>", "<", ">", "g")
returns: ["t<<e>><s>", ""]
matchRecursiveRegExp("<div id=\"x\">test</div>", "<div\\b[^>]*>", "</div>", "gi")
returns: ["test"]
*/
function matchRecursiveRegExp (str, left, right, flags) {
var f = flags || "",
g = f.indexOf("g") > -1,
x = new RegExp(left + "|" + right, "g" + f),
l = new RegExp(left, f.replace(/g/g, "")),
a = [],
t, s, m;
do {
t = 0;
while (m = x.exec(str)) {
if (l.test(m[0])) {
if (!t++) s = x.lastIndex;
} else if (t) {
if (!--t) {
a.push(str.slice(s, m.index));
if (!g) return a;
}
}
}
} while (t && (x.lastIndex = s));
return a;
}
document.write(matchRecursiveRegExp("something start something_needed end something", "start", "end") + "<br/>");
document.write(matchRecursiveRegExp("start something start something end something end start something end", "start", "end")+ "<br/>");
document.write(matchRecursiveRegExp("start something start start something end something start end something end something end something start something end", "start", "end")+ "<br/>");
这篇关于获取2个单词之间的字符串,其中也包含这些单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!