Python Tips : Regular Expression usage to exclude something

Tags: ,
Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Let’s say we want to match every single rain variation ..

“Isolated rain over coastal area” is rain.
“Scattered rain over inland area” is rain.
“Rain” is rain
However..
but not “No rain” .. is Fair weather ..

#!/usr/bin/python
import re
z = 1
y = 2
x = 3
 
if (z == 1) & (x == 3) & (y == 2):
 print "match"
else:
 print "not match"
 
norain = 'No rain'
rain = 'Rain'
rainfall = 'isolated rain interne'
 
zrain  =re.compile ('(?<!No\s)[Rr]ain')
m_rain = zrain.search (rainfall)
m2_rain = zrain.search (rain)
m1_rain = zrain.search (norain)
if m_rain :
  print "got rainfall"
if m2_rain :
  print "got me : Rain"
if m1_rain:
  print "no rain also defined as rain"
else :
        print "No rain is :not raining OK!"

result would be something like follows :

Sharing is caring
Sharing is caring

About Namran Hussin

a soft spoken guy... with exceptional interest in computers and technology. I love to learn new thing and also love to break thing for the sake of learning.. but I do abide to the self-imposed limitation or certain thing such as social thing in life, thing can be done and thing that must be avoided at whatever cost such as drug,illegal tracking, smoke,illicit activity..etc.muahahaha let's share what we had in this short term of the life.! make it worth of the living.~

%d bloggers like this: