Python 3 Programming Tutorial - Regular Expressions / Regex with re

Python 3 Programming Tutorial – Regular Expressions / Regex with re



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: http://pythonprogramming.net/beginner-python-programming-tutorials/

Python 3 Programming tutorial Playlist: http://www.youtube.com/watch?v=oVp1vrfL_w4&feature=share&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M

http://seaofbtc.com
http://sentdex.com
http://hkinsley.com
https://twitter.com/sentdex

Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6

source

48 comments

  1. how about you tell us where you are looking this shit from…….then we wouldn't have to go through your video at all.
    i like your videos but this shit i noticed its not good bro. We look at your teaching expecting you know these things to your skin. Even i could look at some place a teach it on a video. At least be prepared.

  2. Great tutorial as always. I spotted a few comments about using dict(zip(names, ages)) instead of the loop which is the most concise way I know.

    I just wanted to say that if you do ever find that you need the index of the item as you loop through a list, you can use enumerate():

    for index, value in enumerate(values):
    print(index, value)

    That way you can't accidentally forget to increment your counter.

  3. '''IDENTIFIERS:

    d = any number
    D = anything but a number
    s = space
    S = anything but a space
    w = any character
    W = anything but a character
    . = any character, expect for a newline
    b = the whitespace around words
    . = a period

    MODIFIERS:

    {1,3} we're expecting 1-3
    + match 1 or more
    ? match 0 or 1
    * match 0 or more
    $ match the end of a string
    ^ matching the beginning of a string
    | either or
    [] range or "variance" [1-5a-qA-Z]
    {x} expecting "x" amount

    WHITE SPACE CHARACTERS:

    n new line
    s space
    t tab
    e space
    f form feed
    r return

    DONT FORGET!:
    . + * ? [ ] $ ^ ( ) {} |
    '''
    import re
    exampleString = '''
    Potato is 15 years old,and Tomato is 27 years old.
    Orange is 97,and his grandfather,Lemon, is 102.
    '''
    ages = re.findall(r'd{1,3}',exampleString)
    names = re.findall(r'[A-Z][a-z]*',exampleString)

    print("Ages: ",ages)
    print("Names: ",names)
    x = 0
    ageDict = {}
    for eachName in names:
    ageDict[eachName] = ages[x]
    x+=1
    print("All: ",ageDict)

  4. Battle Sniper
    1 second ago
    Ages: ['15', '27', '97', '102']
    Names: ['Potato', 'Tomato', 'Orange', 'Lemon']
    All: {'Potato': '15', 'Tomato': '27', 'Orange': '97', 'Lemon': '102'}

  5. See what I got ?????

    import re
    >>> string = '''
    Jessica is 15 years old, and Daniel is 27 years old.
    Edward is 97 years old, and his grandfather, Oscar, is 102.
    '''
    >>> ages = re.findall(r'd{1,3,}',string)
    >>> names = re.findall(r'[A-Z] [ a-z]*',string)
    >>> print (ages)
    []
    >>> print (names)
    []
    >>>

  6. I figured out how to match it in one expression and keep the person's name and age in a tuple within the list:

    print(re.findall(r'([A-Z][a-z]*)D+(d{1,3})', exampleString))

    returns [('Jessica', '15'), ('Daniel', '27'), ('Edward', '97'), ('Oscar', '102')]

    So basically: (capital letter followed by any number of lowercase letters) followed by 1 or more non-digit characters, followed by (any number containing 1-3 digits)

    Only the parts in parentheses are returned.

  7. Easier Way to do this could be:

    import re
    exampleString = '''
    Jessica is 15 years old, and Daniel is 27 years old.
    Edward is 97 years old his grandfather, Oscar is 102.
    '''
    ages = re.findall(r'd{1,3}',exampleString)
    names = re.findall(r'[A-Z][a-z]*',exampleString)
    name = ''.join(names) #Converts the list into string
    x = 0
    for name in names:
    print(name+" is "+ages[x]) # So for every name in names, print name and age
    x = x+1 #Used as index variable in ages[x]

    Output –
    Jessica is 15
    Daniel is 27
    Edward is 97
    Oscar is 102

  8. hi, how can you extract certain stuff from an external file? For example in my external txt file I have a row saying ; January 9.54, and I only want that number to be extracted. How would the code look like? tnx

  9. 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.

  10. 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)?

  11. 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.

Comments are closed.