Python 3 Programming Tutorial – Regular Expressions / Regex with re

///Python 3 Programming Tutorial – Regular Expressions / Regex with re

Python 3 Programming Tutorial – Regular Expressions / Regex with re

FavoriteLoadingAdd to favorites

Regular expressions are used to sift through text-based data to find things. Regular expressions express a pattern of data that is to be located. Regex is its own language, and is basically the same no matter what programming language you are using with it.

In Python 3, the module to use regular expressions is re, and it must be imported to use regular expressions.

Sample code for this basics series:

Python 3 Programming tutorial Playlist:

Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6

source

By |2019-07-24T21:26:37+00:00July 24th, 2019|Python Video Tutorials|23 Comments

23 Comments

  1. Rishang Prashnani July 24, 2019 at 9:26 pm - Reply

    typing sound is so annoying

  2. Darren Lefcoe July 24, 2019 at 9:26 pm - Reply

    Good starter video.

    the for loop at the end is better served this way:

    ageDict = {}

    for i, n in enumerate(names):

    ageDict[n] = ages[i]

    print(ageDict)

  3. Tttt Y July 24, 2019 at 9:26 pm - Reply

    great vid! sub right away. btw, where is your next vid "url lib + regex"? thanks

  4. shalini sahni mankatalia July 24, 2019 at 9:26 pm - Reply

    The video is really good but why don't you use the HTMLParser module for parsing html? It is inbuilt and is really efficient and also saves loads of lines of code

  5. Naam Ke Poojary July 24, 2019 at 9:26 pm - Reply

    OCD attack at 5:43

  6. Sapython Programmer July 24, 2019 at 9:26 pm - Reply

    The best I can get from a YouTube. You are best in teaching python. Amazing tutorials keep making these types of video.

  7. Easier to zip over 2 lists

  8. Saud Nm July 24, 2019 at 9:26 pm - Reply

    good lecture dude

  9. Neel Deshmukh July 24, 2019 at 9:26 pm - Reply

    Am i only the one who thinks he is Mark Zuckerberg's lost twin????

  10. Timothée Volpi July 24, 2019 at 9:26 pm - Reply

    Do you live in Nebraska ?

  11. I am THE ZUCC July 24, 2019 at 9:26 pm - Reply

    5:02 when you wanna join KKK

  12. Carl Badawi July 24, 2019 at 9:26 pm - Reply

    Timeless

  13. d p July 24, 2019 at 9:26 pm - Reply

    And here's a printout of the entire video for people who want to run it in their IDE:

    # –– coding: utf-8 –
    """
    RegEx Tutorial
    """
    import re
    #print(dir(re))
    sb = "-"*50
    print("IDENTIFIERS:")
    print()
    print(" used to escape a character")
    print("d any number")
    print("D anything but a number")
    print("s space")
    print("S anything but a space")
    print("w any character")
    print("W anything but a character")
    print(". any character except a new line")
    print(". actually a period")
    print(r"b whitespace around words")
    print(sb)
    print("MODIFIERS:")
    print()
    print("{1,3} we're expecting 1-3")
    print("+ Match 1 or more")
    print("? Match 0 or 1")
    print("* Match 0 or more")
    print("$ match the end of a string")
    print("^ match the beginning of a string")
    print("| matches either or e.g. d{1-3}|w{5-6}")
    print("[] Match range or 'variance' (e.g. [A-Z][a-z] or [1-5][a-q][A-Z] would return 'Dr' or '3pO')")
    print("{x} expecting 'x' amount")
    print(sb)
    print("WHITE SPACE CHARACTERS:")
    print(r"n new line")
    print(r"s space")
    print(r"t tab")
    print(r"e escape (rare)")
    print(r"f form feed (rare)")
    print(r"r return")
    print()
    print("DON'T FORGET!:")
    print("To use: . + * ? [ ] $ ^ ( ) { } | you have to 'escape' them first!!!")
    print(sb)
    print("EXAMPLES:")
    print()
    exString = """
    Jessica is 15 years old, and Daniel is 27 years old.
    Edward is 97 and his grandfather, Oscar, is 102.
    """
    print("We will use the following example string:")
    print(exString)
    print("To find all ages in the string from 1 to 3 digits, we can create a variable 'ages' = re.findall(r'd{1,3}', exString):")
    ages = re.findall(r'd{1,3}', exString)
    print("ages =", ages)
    print()
    print("To find all names in the string, we can create a variable 'names' = re.findall(r'[A-Z][a-z]*', exString):")
    names = re.findall(r'[A-Z][a-z]*', exString)
    print("names =", names)
    print()
    print("To map each name to their ages, we can do the following:")
    mapping = """
    ageDict = {}
    x = 0
    for eachName in names:
    ageDict[eachName] = ages[x]
    x+=1
    print(ageDict)
    """
    print(mapping)
    ageDict = {}
    x = 0
    for eachName in names:
    ageDict[eachName] = ages[x]
    x+=1
    print("which gives us:")
    print(ageDict)

  14. d p July 24, 2019 at 9:26 pm - Reply

    For anyone who want to print out the RegEx list he types out:

    sb = "-"*50
    print("IDENTIFIERS:")
    print()
    print(" used to escape a character")
    print("d any number")
    print("D anything but a number")
    print("s space")
    print("S anything but a space")
    print("w any character")
    print("W anything but a character")
    print(". any character except a new line")
    print(". actually a period")
    print(r"b whitespace around words")
    print(sb)
    print("MODIFIERS:")
    print()
    print("{1,3} we're expecting 1-3")
    print("+ Match 1 or more")
    print("? Match 0 or 1")
    print("* Match 0 or more")
    print("$ match the end of a string")
    print("^ match the beginning of a string")
    print("| matches either or e.g. d{1-3}|w{5-6}")
    print("[] Match range or 'variance' e.g. [A-Za-z] or [1-5a-qA-Z]")
    print("{x} expecting 'x' amount")
    print(sb)
    print("WHITE SPACE CHARACTERS:")
    print(r"n new line")
    print(r"s space")
    print(r"t tab")
    print(r"e escape (rare)")
    print(r"f form feed (rare)")
    print(r"r return")
    print()
    print("DON'T FORGET!:")
    print(". + * ? [ ] $ ^ ( ) { } |")
    print(sb)

  15. Trillionaire Sanai July 24, 2019 at 9:26 pm - Reply

    You are the best youtuber teacher I have ever encountered. Terrifiedly terrific job done man!

  16. CHOKKAM REDDY SUNKRISHNA July 24, 2019 at 9:26 pm - Reply

    I have a file containing records in format:

    My username is Ajay. I am 20 years old.
    My username is atb00ker. I am 40 years old.
    My username is 394843. I am 22 years old.

    There are 100 such records.
    You have to write a regex to match records of people of age below 20.
    Send the regex expression as submission.

  17. Harnish Shah July 24, 2019 at 9:26 pm - Reply

    how can i add multiple flags in same line? for example in this suppose i want only ages so make re. for only ages , and now i want name also so make re. for that.so hence i want either only ages or name. so how can i combine this both re. with (OR option)?

  18. mehdi jafari July 24, 2019 at 9:26 pm - Reply

    short and clear.
     thanks!

  19. Ujjawal Panchal July 24, 2019 at 9:26 pm - Reply

    At age 5, Oscar became a grandfather! I didn't know grandfather's spelling at age 5! Damn!

  20. Sairaj Das July 24, 2019 at 9:26 pm - Reply

    for eachName, eachAge in zip(ages, names):
    ageDict[eachName] = eachAge

  21. Md. Nazmul Hossain July 24, 2019 at 9:26 pm - Reply

    how can I take multi-line string input from user and remove all digit from it.

  22. trouser python July 24, 2019 at 9:26 pm - Reply

    Sentdex looks and sounds hungover af in this one.

  23. sadegh rizi July 24, 2019 at 9:26 pm - Reply

    How could we write a regular expression that says we're looking for a string that contains at least one character among[A-Z] and the other characters are among [a-z]? Knowing the answer would be useful for writing a program to test the security of a password using regular expressions.

Leave A Comment

*