【发布时间】:2017-05-01 06:53:57
【问题描述】:
我正在尝试查找重叠字符串的多个匹配项,并带有单词边界。一旦找到一个子串,它就不会被考虑用于未来的匹配,即下一次搜索将在该子串的结尾之后开始。例如,我需要此字符串的这些匹配项:
pattern: "ab ab"
string: "ab ab abxxxab ab ab"
----- -----
^ ignore this, since it is not a word boundary
substr found: (0 4)
substr found: (14 18)
我编写了以下代码,但它只找到第一个子字符串。问题是在拒绝第二个匹配后(由于单词边界),它没有找到第三个匹配,这将是一个合法的子字符串。
我得到的输出如下:
string is 0 18<ab ab abxxxab ab ab>
match found:start=0 end=4
substr found: (0 4)
string is 5 18<ab ab abxxxab ab ab>
match found:start=0 end=4
match found:start=11 end=15
(1) 如何解决此正则表达式中的问题,以便也考虑第 3 次匹配? (2) 我正在使用显式 C 代码处理字边界检查,这可以作为正则表达式本身的一部分来完成吗?
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int find_substr(string str, regex pat, int start) {
int last = str.length() - 1;
printf("string is %d %d<%s>\n", start, last, str.c_str());
for(auto it = sregex_iterator(str.begin(), str.end(), pat);
it != sregex_iterator(); ++it) {
int idx = it->position();
int end = idx+ it->length() - 1;
printf("match found:start=%d end=%d\n", idx, end);
if(idx<start) {
continue; //ignore matches before the start index
}
if(idx>0) {
if((str.at(idx-1)>='a' && str.at(idx-1)<='z') ||
(str.at(idx-1)>='A' && str.at(idx-1)<='Z')) {
continue; // not a word boundary, ignore
}
}
if(end<last) {
if((str.at(end+1)>='a' && str.at(end+1)<='z') ||
(str.at(end+1)>='A' && str.at(end+1)<='Z')) {
continue; // not a word boundary, ignore
}
}
printf("substr found: (%d %d)\n", idx, end);
return end+1;
}
return -1;
}
int main() {
string str;
regex pat;
int next;
str = "ab ab abxxxab ab ab";
pat = "ab ab";
next = find_substr(str, pat, 0);
if(next>0 && next<str.length()) {
find_substr(str, pat, next);
}
}
【问题讨论】: