You are on page 1of 1

Regular Expression Last Checkpoint: 13/10/2023 (autosaved) Logout

File Edit View Insert Cell Kernel Widgets Help Trusted Python 3 (ipykernel) 

 +       Run    Code 

In [ ]: #Regular Expressions

In [54]: import re
pattern = '^h.{7}s' # words starting with h and ending with s. its a 9 letter word
test_string = 'happins'

result = re.match(pattern, test_string)


print(result)

if result:
print("Search successful")

else:
print("Search unsuccessful")

None
Search unsuccessful

In [30]: import re

txt = "There is no path to happiness; happiness is the path"

a = re.search("in", txt)
b = re.findall("in", txt)
c = re.match("Th", txt)
d = re.match("in", txt)

print(a) #this will print an object


print(b) #this will print the list of all matches
print(c)
print(d)

<re.Match object; span=(24, 26), match='in'>


['in', 'in']
<re.Match object; span=(0, 2), match='Th'>
None

In [32]: import re

txt = "There is no path to happiness; happiness is the path"


x = re.split("\s", txt)

print(x) #this will print a list of words

['There', 'is', 'no', 'path', 'to', 'happiness;', 'happiness', 'is', 'the', 'path']

In [34]: #a regular expression to search digit inside a string

You might also like