Skip to main content

regex in python python hunter

Regular Expressions is a powerful concept if understood clearly you can save your valuable time to extract out the particular text from huge string or paragraph.

It is wildly pronounced as regex, it can help you to automate the boring stuff, like searching particular text form log files, python used this same concept in web scrapping.

Below are some common examples to understand the regex.
Method names will help you to have an idea that what exactly the regex is intended to do.

if you don't understand any regex or want to create your own regex with your requirements then you can comment below, i'll reach you ASAP.
Thanks.



import re
def phoneNumberPattern():
	print("*" * 10)
	print("phoneNumberPattern()")
	regexObj = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
	mo = regexObj.search('Find my phone number from this string 998-805-4332')
	print(mo.group())


def grouping():
	print("*" * 10)
	print("grouping()")
	regexObj = re.compile(r'(\(\d\d\d\))-(\d\d\d-\d\d\d\d)')
	mo = regexObj.search('Find my phone number from this string (998)-805-4332')

	print(mo.group())
	print(mo.group(1))
	print(mo.groups(2))


def multipleGroups():
	print("*" * 10)
	print("multipleGroups()")
	mg = re.compile(r'THE_PATH|THE_FAITH')
	mo = mg.search("My best book it THE_PATH and my friend reads THE_FAITH")
	print(mo.group())


def optionalMatching():
	print("*" * 10)
	print("optionalMatching()")
	op = re.compile(r'Micro(particles|waves|)')
	mo = op.search("Data from which the string needs to be searched with Microwaves")
	print(mo.group())


def optionalMatchingWithQuestionMark():
	print("*" * 10)
	print("optionalMatchingWithQuestionMark()")
	op = re.compile(r'Spider(wo)?Man')
	mo = op.search('I want this string to be matched, SpiderMan')
	print(mo.group())


def zeroOrMoreWithStar():
	print("*" * 10)
	print("zeroOrMoreWithStar()")
	op = re.compile(r'Spider(wo|w)*Man')
	mo = op.search("Data is SpiderwowowowoMan")
	print(mo.group())

def OneOrMoreWithPlus():
	print("*" * 10)
	print("OneOrMoreWithPlus()")
	op = re.compile(r'Spider(wo|w)+Man')
	mo = op.search("Data is SpiderwowwwwwMan")
	print(mo.group())

def repetition():
	print("*"*10)
	print("repetition()")
	op = re.compile(r'e{2}')
	mo = op.search("His name is sandeep, which is correct")
	print(mo.group())

def greedyAndNongreedy():
	print("*"*10)
	print("greedyAndNongreedy()")
	greedy = re.compile(r'(Foo){3,6}')
	nonGreedy = re.compile(r'(Foo){3,6}?')
	greedy = greedy.search("This is data with FooFooFooFooFooFoo")
	
	nonGreedy = nonGreedy.search("This is data with FooFooFooFooFooFoo")
	print(greedy.group())
	print(nonGreedy.group())

def characterClass():
	print("*"*10)
	print("characterClass()")
	op = re.compile(r'[aeiou]')
	neg = re.compile(r'[^aeiou]')
	mo = op.findall("Find the vowels from this string")
	neg_mo = neg.findall("will not find any vowel from this string")
	print(mo)
	print(neg_mo)
	
	op = re.compile(r'\d+\s\w+')
	mo = op.findall("01 Hitman 02 PythonHunter 03WillNotBeFound")
	print(mo)

 

def carrotAndDollar():
	print("*"*10)
	print("carrotAndDollar()")
	op = re.compile(r'\d$')
	mo = op.search("This string ends with number 88")
	print(mo.group())
	
	op = re.compile(r'^\d$')
	mo = op.search("2")
	print(mo.group())
	
	op = re.compile(r'^\d+$')
	mo = op.search("34443")
	print(mo.group())
	
	op = re.compile(r'^\d*$')
	mo = op.search("")
	print(mo.group())

def wildCard():
	print("wildCard()")
	print("*"*10)
	op = re.compile(r'.lay')
	mo = op.findall("play relay clay")
	print(mo)
	
	op = re.compile(r'.*')
	mo = op.search("play relay clay")
	print(mo.group())
	
	op = re.compile(r'{.*}')
	mo = op.search('{Hitman is on} his mission.}')
	print(mo.group())
	
	op = re.compile(r'{.*?}')
	mo = op.search('{Hitman is on} his mission.}')
	print(mo.group())

def complexReg():
	print("*"*10)
	print("complexReg()")
	op = re.compile(r'((\d{3}|\(\d{3}\))?(\s|-|\.)?\d{3}(\s|-|\.)\d{4}(\s*(ext|x|ext.)\s*\d{2,5})?)')
	mo = op.search("343-343-3544")
	print(mo.group())
	
	mo = op.search("(343)-343-3544")
	print(mo.group())
	
	mo = op.search("343 343-3544")
	print(mo.group())
	
	mo = op.search("343     343-3544")
	print(mo.group())
	
	mo = op.search("343-343-3544 ext 44")
	print(mo.group())
	
	mo = op.search("343-343-3544   ext  44")
	print(mo.group())
	
	mo = op.search("343-343-3544   ext.  44")
	print(mo.group())

def caseSensitive():
	print("*"*10)
	print("caseSensitive()")
	op = re.compile(r'.lay', re.I)
	mo = op.findall("play relay clay or PLAY RELAY CLAY")
	print(mo)

def findString():
	print("*"*10)
	print("findString()")
	op = re.compile(r'\w*ee\w*')
	mo = op.findall("Find the ee from this string where my name \is written as sandeepppp and go deep and keep the things in \deep creep")
	print(mo)
	
	op = re.compile(r'((\w*ee\w*)|(\s*ee\s*))')
	mo = op.findall("Find the ee from this string where my name \is written as sandeepppp and go deep and keep the things in \deep creep")
	print(mo)
	
	op = re.compile(r'(\w*ee\w*)|(\s*ee\s*)')
	mo = op.findall("Find the ee from this string where my name \is written as sandeepppp and go deep and keep the things in \deep creep")
	print(mo)
	#print(mo.group())

def substitution():
	print("*"*10)
	print("substitution()")
	op = re.compile(r'Changed (\w)(\w)\w*')
	result = op.sub(r'\2****','This is the Changed String')
	print(result)

def datePattern():
	print("*"*10)
	print("datePattern()")
	op = re.compile(r'(\d{2})\/([a-zA-Z]{3})\/20(\d{2})')
	mo = op.search("78/NON/2018")
	print(mo.group())
	
	op = re.compile(r'^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$')
	mo = op.search("2018.12.31")
	print(mo.group())

def emailAddress():
	print ("*"*10)
	print("emailAddress")
	print(sys.argv[1])
	op = re.compile(r'[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z0-9]{2,5}')
	mo = op.search(sys.argv[1])
	if mo is None:
		print("mo is none")
	else:
		print(mo.group())
		mo = op.findall(sys.argv[1])
		print(mo)
		
def repitingGroups():
	op = re.compile(r'([0-9a-zA-Z]+_?)*([0-9a-zA-Z]$)')
	mo = op.search("BRPROD1_HGI_YHY2")
	print(mo.group())
  
def callingAllFunction():
	print("Calling All Functions")
	datePattern()
	phoneNumberPattern()
	grouping()
	multipleGroups()
	optionalMatching()
	optionalMatchingWithQuestionMark()
	zeroOrMoreWithStar()
	OneOrMoreWithPlus()
	repetition()
	greedyAndNongreedy()
	characterClass()
	carrotAndDollar()
	wildCard()
	complexReg()
	caseSensitive()
	findString()
	substitution()
	repitingGroups()

	
callingAllFunction()
	
	
if __name__ == "__main__":
	callingAllFunction()

  

Comments

Popular posts from this blog

Understanding "with" keyword in python, on Python Hunter

J ust like anything in python, keyword "with" is introduced in python to make the things little easy. Imagine a situation where you have to manage the resources e.g opening file and closing them after the code is executed on file. To achieve this sort of task we have to write the code as follow:      Download   But if you do it often you could do this as follow to make the code reusable: Download But why do you need to do this when you know that you have to execute the only for once.  To answer this question  python-dev team finally came up with following approach: Download  Note:- Make sure you have "file.txt" and python code file in same dir. The "with"  keyword replaces the try finally block. "with" keyword executes the openFileClass() context manager and internally calls the __enter__(self) method, and whatever is being returned from __enter__(self) method is being stored in tar

Understanding the usage of underscore( _ ) of Python for beginner. On Python Hunter

Introduction: Just like you, a newbie in python scripting language, me too was confused about lot of new things in python that are not valid or available in other languages like Java, .Net etc. Just like other things i had seen the use of '_' underscore in python, at beginning level that flabbergasted me for a while.      With some research and practice i have summarised the following usage of '_' underscore in python. Hope you will find it helpful at beginning level. First Usage : Hold the previous output value. When used in interpreter. 1 2 3 4 5 6 7 _ = input () # For example you typed '5' print (_) # This will print '5' print ( int ( _ ) * int ( _ ) ) # This will print '25' print ( int ( _ ) + 10 ) The above will print '15', because last input was "5" and in above   line of code is producing '25' as output but not being handl

XSLT apply import tag by pran sukh on python hunter blog

Modular Programming is good attitude of best programmer. We often need to keep our code in modular approach so that is would be easy to maintain  and update or remove dead code and also it ease the process of bug tracking. XML and XSL  processors provide freedom to import multiple imports to process the same  XML  document. In the following  XML  data we have data from collage where student's and teacher's data is given. But we want to process the same  XML  data with different XSL files. Here in this example we want to show the teacher data in red background color and student data in green background color. Data.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <?xml version = "1.0"?> <?xml-stylesheet type = "text/xsl" href = "RootXSLT.xsl"?> <data> <DataFor name = "Student" id = "001" > <firstname>