Blog Archive

Saturday, October 28, 2023

Regular Expressions in Python

Regular Expressions in Python || Python Tutorial || Learn Python Programming




import re


string = 'Bruce Lee'

pattern = '^(\w+)\s+(\w+)$' #match firstname lastname

match = re.search(pattern, string)

if match: 

    print(string)        #Bruce Lee

    print(match.span()) #(0,9)

    print(match.group()) #Bruce Lee


pattern = '^(?P<fn>\w+)\s+(?P<ln>\w+)$' #match firstname lastname

match = re.search(pattern, string)

if match: 

    print(string)            #Bruce Lee

    print(match.span())      #(0,9)

    print(match.group('fn')) #Bruce

    print(match.group('ln')) #Lee


pattern = '^[a-zA-Z!]+$'

string='m!sha'

match = re.search(pattern, string)

if match: 

    print(string)            #m!sha


re.search() # return the first match

re.findall(pattern, string, flags=0) # return all non-overlapping matched of pattern in string, as a list of strings or tuples


string='Bruce Lee'

pattern = '[a-z]+' #match firstname lastname

match = re.findall(pattern, string)

if match: 

    print(match)            #['ruce','ee']    


match = re.finditer(pattern, string) #iterator version


re.match(pattern, string, flags=0) #

match(pattern, string, flags=0)

    Try to apply the pattern at the start of the string, returning

    a Match object, or None if no match was found.


re.fullmatch(pattern, string, flags=0) #

fullmatch(pattern, string, flags=0)

    Try to apply the pattern to all of the string, returning

    a Match object, or None if no match was found.

values = ['https://www.socratica.com', 'http://www.socratica.org','not_a_www_https://']

regex = 'https?://w{3}.\w+.(org|com)'

for value in values:

    if re.fullmatch(regex, value):print(value)


https://www.youtube.com/watch?v=K8L6KVGG-7o

https://github.com/CoreyMSchafer/code_snippets/tree/master/Python-Regular-Expressions


import re


emails = '''

CoreyMSchafer@gmail.com

corey.schafer@university.edu

corey-321-schafer@my-work.net

'''


pattern = re.compile(r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+')


matches = pattern.finditer(emails)


for match in matches:

    print(match)

#https://github.com/CoreyMSchafer/code_snippets/blob/master/Python-Regular-Expressions/simple.py

import re


text_to_search = '''

abcdefghijklmnopqurtuvwxyz

ABCDEFGHIJKLMNOPQRSTUVWXYZ

1234567890


Ha HaHa


MetaCharacters (Need to be escaped):

. ^ $ * + ? { } [ ] \ | ( )


coreyms.com


321-555-4321

123.555.1234

123*555*1234

800-555-1234

900-555-1234


Mr. Schafer

Mr Smith

Ms Davis

Mrs. Robinson

Mr. T

'''


sentence = 'Start a sentence and then bring it to an end'


pattern = re.compile(r'start', re.I)


matches = pattern.search(sentence)


print(matches)


##

# https://github.com/CoreyMSchafer/code_snippets/blob/master/Python-Regular-Expressions/urls.py

import re


urls = '''

https://www.google.com

http://coreyms.com

https://youtube.com

https://www.nasa.gov

'''


pattern = re.compile(r'https?://(www\.)?(\w+)(\.\w+)')


subbed_urls = pattern.sub(r'\2\3', urls) #back reference


print(subbed_urls)


# matches = pattern.finditer(urls)


# for match in matches:

#     print(match.group(3))



Example of callable in re.sub()

s='abcdeABCDE'

def star_ord(m):

    return f'*<{m.group(0)}>*'


re.sub('[ae]',star_ord,s)

#'*<a>*bcd*<e>*ABCDE'


reference:

https://docs.python.org/3/library/re.html

 https://www.youtube.com/watch?v=nxjwB8up2gI

https://github.com/CoreyMSchafer/code_snippets/tree/master/Python-Regular-Expressions

https://www.python-engineer.com/posts/regular-expressions/

https://youtu.be/AEE9ecgLgdQ?si=2XZpygF-cFoAL0-i



No comments:

Post a Comment