RegEx with no state

I am trying to use a regex that does the following: matches any text without the word "Chrome", followed by the word "Safari"

I have compiled a python script that does not work.

#!/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" 

I tried the following examples

 test_negative.py "Chrome Mobile/12345 Safari" > match test_negative.py "Like MAC OS Safari" > match 

I was hoping that the first would return “no match,” and the second would return “match.” If someone can help with regex, that would be great, thanks.

+4
source share
3 answers

Can't you just write a regex to match if Safari follows Chrome and then negates the conditional?

 #!/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" 

It seems easier to me.

Results:

 mgilson@iris :~/sandbox$ python test.py "Like MAC OS Safari" match mgilson@iris :~/sandbox$ python test.py "Chrome Mobile/12345 Safari" no match 
+2
source

Consider the following example of a regular expression syntax. This regular expression satisfies your examples, however it uses some images that python cannot resolve.

  • (?<!Chrome.*?)Safari(?!.*?Chrome)|(?<!Safari.*?)Chrome(?!.*?Safari)|(?<!(Chrome|Safari).*?)$ demo'd here, since output 1. fails only if chrome or safari are on the same line

  • (?<!Chrome.*?)Safari|(?<!Safari.*?)$ Demo'd here since output 2. fails only if chrome followed by safari

Example

 $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 

Productivity

 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 

Summary

  • Output

    • (?<!Chrome.*?)Safari(?!.*?Chrome) searches for a Safari word that is preceded or not followed by Chrome
    • | or
    • (?<!Safari.*?)Chrome(?!.*?Safari) searches for the word chrome, which is not preceded or followed by the word Safari
    • | or
    • (?<!(Chrome|Safari).*?)$ Anywhere in the line
  • Print two that satisfy the exact condition in the original question match any text without the word "Chrome" followed by the word "Safari"

    • (?<!Chrome.*?)Safari If safari exists and chrome fails
    • | or
    • (?<!Safari.*?)$ term safari not found in the string
0
source

It works

 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") 

gives

 >>> match 

and

 matcher = negative_re.search( "Chrome MAC OS Safari" ) if matcher: print ("match") else: print ("no match") 

gives

 >>> no match 
0
source

Source: https://habr.com/ru/post/1480137/


All Articles