我有一个这样的字符串:
"result is abcdefg hij!klm </td"
(或其他所有内容而不是abcd ......)
我所制作的正则表达式是:
"result is ([^<]+) </td"
这很有效,因为找到了结果。但是当字符串是:
"result is not found </td"
...我怎样才能避免提取“未找到”这几个字?
我知道有负前瞻表达,但这些在C99中的regex.h中不起作用。
"(?!not found)"
- &gt;坏的正则表达式"([^n][^o][^t][^ ][^f]..)"
- &gt;与“now”不匹配,例如"(([^<]+)&(!not found))"
- &gt;坏
正则表达式(没有'&amp;'运算符,我认为解决方案可以是:a&&b == !a||!b
)
- EDIT--
在这里,您是计算正则表达式的代码的一部分。
pmatch=malloc(nmatch*sizeof(regmatch_t));
printf("regex: %s\n",patrn);
if (regcomp(&rgT,patrn,REG_EXTENDED | REG_NEWLINE) != 0)
{
snprintf(globals.err_buff,MAX_BUFF,"bad regex: \"%s\"",patrn);
w_report_error(globals.err_buff,__FILE__,__LINE__,__func__,0,0,error);
return EXIT_FAILURE;
}
- 编辑 -
也许我找到了解决方案:
我自己的正则表达式函数返回第N个反向引用,如果传递一个数字&gt; 0作为参数,所以...
注意:./regex
只是一个C程序,它将argv [...]重定向到我自己的库的w_regexp。
$ ./regex "result is crack </td" 'result is (not found) </td|result is ([^<]+) </td' 3
regex: result is (not found) </td|result is ([^<]+) </td
"crack"
""
"result is crack </td"
$ ./regex "result is not found </td" 'result is (not found) </td|result is ([^<]+) </td' 3
regex: result is (not found) </td|result is ([^<]+) </td
""
"not found"
"result is not found </td"
所以,我认为在我的结构中添加一个数字,这意味着用于提取数据的反向引用的索引可以是一个解决方案,但我仍然会等待另一天更好的方法,或者2。
提前谢谢。
- 编辑 - (太多次:))
有用!
我把那个我想避免跟随者的字符串放在'|'以及正确字符串的模式
这是正则表达式:
"result is not found </td|result is ([^<]+) </td"
再次感谢。
答案 0 :(得分:0)
也许像"result is (?:not found)?([^<]+)</td"
?
答案 1 :(得分:0)
Aztaroth的作品也是result is ((?!not found)[^<]+) </td
- 不同之处在于他注册了一个空的匹配,我的注册不匹配。
使用
进行测试result is abcdefg hij!klm </td
result is not found </td
result is not this </td
result is note this </td
result is ote this </td
编辑:羞耻,好吧 - 这是懒惰而且有点笨拙但是两个正则表达式怎么样?首先检查“未找到”result is (not found) </td
的匹配项。然后,使用原始正则表达式进行不匹配,删除结果。