1

我需要根据两个要求过滤字符串

1) 他们必须以“city_date”开头

2)他们不应该在字符串中的任何地方都有“metro”。

这需要在一次检查中完成。

一开始我知道它应该是这样的,但不知道用“metro”消除字符串

string pattern = "city_date_"

补充:我需要将正则表达式用于 SQL LIKE 语句。因此我需要它在一个字符串中。

4

3 回答 3

4

使用否定的前瞻断言(我不知道你的正则表达式库是否支持这一点)

string pattern = "^city_date(?!.*metro)"

我还在开头添加了一个锚点^,它将匹配字符串的开头。

(?!.*metro)如果前面某处有字符串“metro”,则否定前瞻断言将失败。

于 2012-10-23T10:37:18.027 回答
3

正则表达式通常比直接比较昂贵得多。如果直接比较可以轻松表达需求,请使用它们。这个问题不需要正则表达式的开销。只需编写代码:

std::string str = /* whatever */
const std::string head = "city_date";
const std::string exclude = "metro";
if (str.compare(head, 0, head.size) == 0 && str.find(exclude) == std::string::npos) {
    // process valid string
}
于 2012-10-23T10:45:28.327 回答
0

通过使用 javascript

input="contains the string your matching"

var pattern=/^city_date/g;
if(pattern.test(input))  // to match city_data at the begining
{
var patt=/metro/g;
if(patt.test(input)) return "false";  
else return input; //matched string without metro
}
else
return "false"; //unable to match city_data
于 2012-10-23T13:25:32.680 回答