Python 不带条件的正则表达式
我尝试使用执行以下操作的正则表达式: 匹配任何不带“Chrome”和“Safari”的文本 我编写了一个无法工作的python脚本Python 不带条件的正则表达式,python,regex,Python,Regex,我尝试使用执行以下操作的正则表达式: 匹配任何不带“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
#!/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"
我希望第一个回答“不匹配”,第二个回答“匹配”。如果有人能帮助您使用正则表达式,那就太好了,谢谢。如果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“像MAC OS Safari”
比赛
mgilson@iris:~/sandbox$python test.py“Chrome Mobile/12345 Safari”
没有对手
考虑以下regex的powershell示例。这个正则表达式满足您的示例,但是它使用了一些python可能不允许的lookarounds
(?此处演示为输出1。仅当
或chrome
位于同一字符串上时失败safari
(?此处演示为输出2。仅当
后跟chrome
safari
- 输出一
(?查找单词Safari,该单词前面或后面没有Chrome
或|
(?查找单词chrome之前或后面没有单词Safari
或|
(?线路上没有任何地方
- 输出满足原始问题中确切条件的两个
匹配任何不带“Chrome”和“Safari”的文本
(?如果
存在并且未通过Safari
Chrome
或|
(?字符串中找不到术语
)safari
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")
给予
及
给予
不幸的是,这会起作用,我无法控制实际的代码。我得到了一个配置文件,可以在其中插入表示匹配的正则表达式字符串。基本上,我所能做的就是修改表达式的白名单。很抱歉,我在提出问题时应该提到这个限制。r'^(?!Chrome.*Safari).+“如果Chrome在开始时就可以了。
$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
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