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
Post a Comment
Thanks in anticipation.