我必须选择包含单词的行one而不是another.这些行形成一些json字符串,如下所示:
{"Name" : "one", "LastError" : "No error", "ID" : 1000 , "Comment" : "no comment"} //YES
{"Name" : "one", "LastError" : "No error", "ID" : 1000 , "Comment" : "another"} //NO because there is 'one' and 'another'
Run Code Online (Sandbox Code Playgroud)
我正在使用php和preg_match.
我想尝试使用像:
if (preg_match('/one.*(?!another)/i',$row_string) > 0)
{
//no draw
}
else
{
//Draw something
}
Run Code Online (Sandbox Code Playgroud)
看起来前瞻没有做任何事情.
你的正则表达式
/one.*(?!another)/
Run Code Online (Sandbox Code Playgroud)
表示匹配字符串one后跟任意数量的字符,后面的字符串.*必须不匹配another.
.*将基本匹配到字符串的结尾,所以它不会跟着another.
你真正想要的是匹配字符串one后跟任意数量的字符,并且每个字符都不能跟随another.
这个工作:
/one(.(?!another))*$/
Run Code Online (Sandbox Code Playgroud)
将$可确保断言对下面的每个字符进行测试one.
为了确保即使one它本身没有another,我们也必须在之后添加断言one:
/one(?!another)(.(?!another))*$/
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3774 次 |
| 最近记录: |