我正在尝试使用执行以下操作的正则表达式: 匹配任何文字,不带“Chrome”字样,后跟单词“Safari”
我把一个无效的python脚本放在一起。
#!/usr/bin/env python
import sys
import re
# any text but 'Chrome' followed by Safari
negative_re = re.compile( '(?<!Chrome).*?Safari' )
matcher = negative_re.search( sys.argv[1] )
if matcher:
print "match"
else:
print "no match"
我尝试了以下示例
test_negative.py "Chrome Mobile/12345 Safari"
> match
test_negative.py "Like MAC OS Safari"
> match
我希望第一个返回“不匹配”,第二个返回“匹配”。如果有人可以帮助使用正则表达式,那就太棒了,谢谢。
答案 0 :(得分:2)
如果 Safari跟随Chrome,然后否定条件,那么你不能只写正则表达式匹配吗?
#!/usr/bin/env python
import sys
import re
# any text but 'Chrome' followed by Safari
negative_re = re.compile(r'Chrome.*Safari')
matcher = negative_re.search(sys.argv[1])
if matcher is None:
print "match"
else:
print "no match"
这对我来说似乎更容易。
结果:
mgilson@iris:~/sandbox$ python test.py "Like MAC OS Safari"
match
mgilson@iris:~/sandbox$ python test.py "Chrome Mobile/12345 Safari"
no match
答案 1 :(得分:0)
考虑以下关于正则表达式的powershell示例。这个正则表达式满足你的例子,但它确实使用了python可能不允许的一些外观。
(?<!Chrome.*?)Safari(?!.*?Chrome)|(?<!Safari.*?)Chrome(?!.*?Safari)|(?<!(Chrome|Safari).*?)$
此处演示为输出1.仅当chrome
或safari
位于同一字符串时才会失败
(?<!Chrome.*?)Safari|(?<!Safari.*?)$
在此处演示为输出2.仅当chrome
后跟safari
$Matches = @()
[array]$input = @()
$input += 'Chrome Mobile/12345 Safari'
$input += 'Like MAC OS Safari'
$input += 'Safari Mobile/12345 Chrome'
$input += 'Like MAC OS chrome'
$input += 'Internet Explorer is deprecated'
$input += 'I like Chrome better then Safari for looking at kittens'
$input += 'Safari is easier to vote with'
$Regex = '(?<!Chrome.*?)Safari(?!.*?Chrome)|(?<!Safari.*?)Chrome(?!.*?Safari)|(?<!(Chrome|Safari).*?)$'
Write-Host Output 1
foreach ($String in $Input) {
if ( $String -imatch $Regex ) {
write "'$String' `t matched"
} else {
write "'$String' `t did not match"
} # end if
} # next
Write-Host
Write-Host Output 2
# but I want to allow for only:
# match any text without the word "Chrome" followed by the word "Safari"
$Regex = '(?<!Chrome.*?)Safari|(?<!Safari.*?)$'
foreach ($String in $Input) {
if ( $String -imatch $Regex ) {
write "'$String' `t matched"
} else {
write "'$String' `t did not match"
} # end if
} # next
Output 1
'Chrome Mobile/12345 Safari' did not match
'Like MAC OS Safari' matched
'Safari Mobile/12345 Chrome' did not match
'Like MAC OS chrome' matched
'Internet Explorer is deprecated' matched
'I like Chrome better then Safari for looking at kittens' did not match
'Safari is easier to vote with' matched
Output 2
'Chrome Mobile/12345 Safari' did not match
'Like MAC OS Safari' matched
'Safari Mobile/12345 Chrome' matched
'Like MAC OS chrome' matched
'Internet Explorer is deprecated' matched
'I like Chrome better then Safari for looking at kittens' did not match
'Safari is easier to vote with' matched
输出一个
(?<!Chrome.*?)Safari(?!.*?Chrome)
查找不在Chrome |
或(?<!Safari.*?)Chrome(?!.*?Safari)
查找单词Safari |
或(?<!(Chrome|Safari).*?)$
不在线上输出两个满足原始问题match any text without the word "Chrome" followed by the word "Safari"
(?<!Chrome.*?)Safari
如果Safari
存在且未由Chrome
继续|
或(?<!Safari.*?)$
在字符串safari
答案 2 :(得分:0)
这是有效的
import sys
import re
# any text but 'Chrome' followed by Safari
negative_re = re.compile( '^(?!Chrome).*(Safari).*$' )
matcher = negative_re.search( "Void MAC OS Safari" )
if matcher:
print ("match")
else:
print ("no match")
给出
>>>
match
和
matcher = negative_re.search( "Chrome MAC OS Safari" )
if matcher:
print ("match")
else:
print ("no match")
给出
>>>
no match