Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Another bunch of my first Python lines
  • Loading branch information
JoeCare authored Apr 22, 2020
1 parent 39aefd9 commit b8c0bb3
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 0 deletions.
80 changes: 80 additions & 0 deletions RegularExpressions/regex1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import re



def findCellPhone(text):
"""Function takes string as an arg and check if it fits expression of the cell phone kind.
Returns that number if occur or False if nothing fits regex."""
regObject = re.compile(r'\d\d\d \d\d\d \d\d\d')
out = regObject.search(text)
if out:
return out.group()
else:
return False
# msg = input("Give me Your phone number.")
# print(findCellPhone(msg))

# MORE GROUPING:

def findFullName(text):
"""Takes string and checks if it contains fullname in given format. Returns that fragment or false
if nothing fits."""
fullName = re.compile(r'[ABCDEFGHIJKLMNOPRTUWXYZ]\w+ [ABCDEFGHIJKLMNOPRTUWXYZ]\w+')
mo = fullName.search(text)
if mo:
return mo.group()
else:
return False


# msg1 = input("Give me Your full name.")
# print(findFullName(msg1))

# GREEDY AND NON-GREEDY REGEX'es:

def findWord(text):
word = re.compile(r'\w{2,}?') # question mark at the end of regex match object indicate
# that match'll be non-greedy (take shortest match)
mo = word.search(text)
if mo:
return mo.group()
else:
return False


msg2 = "Little Dorothy is happy." # input("Give me some words.")
print(findWord(msg2))

# NOTES:
print(type(re.compile(r'\d\d\d')))
print(re.compile(r'\d\d\d'))
# regular expression Pattern class

print(type((re.compile(r'\d\d\d')).search("ABCD1234")))
print((re.compile(r'\d\d\d')).search("ABCD1234"))
# regular expression Match class

print(type((re.compile(r'\d\d\d')).search("ABCD1234").group()))
print((re.compile(r'\d\d\d')).search("ABCD1234").group())
# regular expression grouped Match - string class

print((re.compile(r'\d*')).search("ABCD1234").group())
# * sign after character class means ZERO OR MORE occurances needed for match
print((re.compile(r'\d+')).search("ABCD1234").group())
# +
print((re.compile(r'[awC123]')).search("ABCD1234").group())

print((re.compile(r'[^\d]')).search("ABCD1234").group())
# ^ sign on the beggining of class character bracket [] is negation of that class
print((re.compile(r'^\w')).search("ABCD1234").group())
# ^ sign on the beggining of raw string of expression means the match must start with that character class
print((re.compile(r'\d$')).search("ABCD1234").group())
# $ sign on the end of the rstring of regex means searched expression must end with that character class
print((re.compile(r'^\w+$')).search('ABCD1234A').group())
# ^ on the beggining and $ on the end of pattern indicates that searched string
# has to begin and end with particular character class
print((re.compile(r'.\w.\d.')).search('ABCD1234A').group())
# . sign is a Joker In The Pack, it'll match any character class except newline
# so here it matches first letter (C) before which there is some character (B) as well as after it (D)
# then as a digit 1 is a match and 2 is another wildcard character

57 changes: 57 additions & 0 deletions RegularExpressions/regex2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# MATCH (almost) EVERYTHING:
import re


def match_all(text):
"""Function look for string with First Name: and matches with everything which after it to the
newline sing and then Last Name: with everything further. It also grouping this two wild matches into
two separate strings in tuple.
"""
al = re.compile(r'First Name: (.*)\nLast Name: (.*)')
mo = al.search(text)
if mo:
return mo.group(1,2)
else:
print(al, mo)
return False


msg = "First Name: John\nLast Name: Doe"
print((match_all(msg)))
# .* signs matches with zero or more of whatever character class except newline so if newline would appear
# in above example i.e. after : whole expression wouldn't match and return None. .* works in greedy way;
# if we need non-greedy matching it should be:

nonGreedy = re.compile(r'<.*?>')
mo = nonGreedy.search("<Typing here> is more important > than here.")
print(mo.group())
# that regex pattern matches with string in which there is triangular
# bracket < with any characters classes in it > but it stops after
# first match of such a construct and ignore rest of the searched string
# so GREEDY regex tries to match longest possible string while
# NON-GREEDY matches shortest.

al1 = re.compile(r'First Name:(.*?)').search(msg)
print(al1, al1.group())
# trying non-greedy that way seems to be completely pointless
# cuz * 'zero or more' stops on zero as minimal matching length and takes nothing after : sign


# MATCH (really) EVERYTHING:
# re.DOTALL flag as second argument to the compile() method
def match_really_all(text):
everythingRegex = re.compile(r'.*',re.DOTALL)
mo1 = everythingRegex.search(text)
if mo1:
return mo1.group()
else:
return False


msg = "one2\n3four\nfivesix\n7\n"
print(match_really_all(msg))
# OR obviously shorter without function (but functions are good
# cuz once written we may use them later):
everythingRegex = re.compile(r'.*',re.DOTALL)
mo1 = everythingRegex.search("sy712t8\n217\nshk18Na")
print(mo1.group())
68 changes: 68 additions & 0 deletions RegularExpressions/regex3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# CASE INSENSITIVE REGEX:
import re


def take_pattern(regex):
mo = regex.search("hahaha")
if mo:
return mo.group()
else:
return False


rege1 = re.compile('Hahaha')
rege2 = re.compile('HAHAHA')
rege3 = re.compile('hAhAHa', re.I)
# flag re.I or re.IGNORECASE as 2nd arg match strings ignoring if letters are big or small

print(take_pattern(rege1), take_pattern(rege2), take_pattern(rege3))


# SUBSTITUTING TEXT:
# cellNum = "My cellphone numbr is: 506 789 891"
# print(findCellPhone(cellNum))
# subsRegex = re.sub(r'\d\d\d \d\d\d \d\d\d',r'(26)\d\d\d \d\d\d \d\d\d',cellNum)
# print(subsRegex)
# my trial 1th from a scratch

def substitution(text):
pattern = re.compile(r'.\d\d\d?')
subst = pattern.sub('(+26)',text)
if subst:
return subst
else:
return False

cellNum = "My cellphone numbr is: 506 789 891"
rege4 = re.compile(r'\d\d\d \d\d\d \d\d\d').search(cellNum)
print(rege4.group())
print(substitution(cellNum))

namesReg = re.compile(r'Agent (\w)\w*')
subReg = namesReg.sub(r'\1****', 'Agent Alice gave a gun to Agent Bob.')
print(subReg)
# thaaat is preety strange shit... substitution so changing sth to sth with dedicated .sub() method
# but here first we need to declare compiled regex 'Agent \w+' - which matches string:
# 'Agent, space, single string of one or more letters'; than on that regex we use method .sub()
# of which first argument is a string that replaces what above regex match and second is
# normal string to which we input the changes
regex = re.compile(r'insert \d\w+ \w+')
sub1 = regex.sub('|insertion|','Oryginal string where we insert 1th arg in place that matches regex.')
print(sub1)
sub2 = re.compile('.$').sub(' -',sub1)
print(sub2, re.compile('re.compile').sub('', str(regex).replace("(","").replace(")",".")))

namesReg = re.compile(r'(Agent) (\w)\w*')
subReg = namesReg.sub(r'\2 ****', 'Agent Alice gave a gun to Agent Bob.')
print(subReg)
# so the regex indicate chunk with Agent, space, letter as a regex group, letter zeroormore
# for which we look in 2nd arg of .sub() and replaces it with its 1th arg
# Agent string is somehow completely erased and
# after space we look for letter to match our group in bracket which is just single letter
# than we look for zeroormore letters in a row and its our whole regular expression until
# non-letter sign (like space); whole regex match should be replaced with .sub()'s 1th argument but in it
# we used \1 which indicates first group of matched expression itself (which normally would point just what
# to get rid of for sake of .sub's 1th) will be a part of substituting string
# by using () brackets to create group we distinguish that part of regex for further use


16 changes: 16 additions & 0 deletions RegularExpressions/regex4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import re


phoneRegex = re.compile(r'((\d{3}|\(\d{3}\))?(\s|-|\.)?\d{3}(\s|-|\.)\d{4}(\s*(ext|x|ext.)\s*\d{2,5})?)')
phoneNum = phoneRegex.search("(222)-555 4321 ext.2345")
print(phoneNum.group())
pRegexVerbo = re.compile(r'''(
(\d{3}|\(\d{3}\))? # area
(\s|-|\.)? # separator
\d{3} # 1th chunk
(\s|-|\.) # separator
\d{4} # 2nd chunk
(\s*(ext|x|ext.)\s*\d{2,5})? # extension
)''',re.VERBOSE)

print(pRegexVerbo.search("(222)-555 4321 ext.2345").group())

0 comments on commit b8c0bb3

Please sign in to comment.