4

次のアクションを実行する正規表現を使用しようとしています。「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

最初に「一致なし」を返し、2番目に「一致」を返すことを望んでいました。誰かが正規表現を手伝ってくれたら、それは素晴らしいことです、ありがとう。

4

3 に答える 3

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
于 2013-05-10T15:34:48.183 に答える
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
于 2013-05-10T16:57:10.837 に答える
0

次の正規表現の PowerShell の例を考えてみましょう。この正規表現はあなたの例を満たしていますが、Pythonでは許可されていない可能性のあるいくつかのルックアラウンドを使用しています.

  • (?<!Chrome.*?)Safari(?!.*?Chrome)|(?<!Safari.*?)Chrome(?!.*?Safari)|(?<!(Chrome|Safari).*?)$ ここで出力 1 としてデモします。chromeまたはsafariが同じ文字列にある場合にのみ失敗します。

  • (?<!Chrome.*?)Safari|(?<!Safari.*?)$出力 2 としてここでデモしますchromesafari

$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

概要

  • 出力 1

    • (?<!Chrome.*?)Safari(?!.*?Chrome) Chrome が前後にない単語 Safari を探します
    • | また
    • (?<!Safari.*?)Chrome(?!.*?Safari)Safari という単語が前後に付いていない chrome という単語を探します。
    • |また
    • (?<!(Chrome|Safari).*?)$どこにもない
  • 元の質問の正確な条件を満たすものを 2 つ出力してくださいmatch any text without the word "Chrome" followed by the word "Safari"

    • (?<!Chrome.*?)SafariSafariが存在し、によって進められていない場合Chrome
    • |また
    • (?<!Safari.*?)$用語safariが文字列に見つかりません
于 2013-05-10T16:02:13.510 に答える