Quick actions

cmd+k|ctrl+k

Navigation

Languages

RegEx (Regular Expression)

Snippet info

Language

Python

Visibility

public

Author

hzx33737

Created

2023-05-17T03:19:16.598107Z

Updated

2023-05-17T04:05:15.570176Z

# importing build in module regular expression, re module
import re

string = "search this inside of this text"

a = re.search("this", string)

print(a) 
# <re.Match object; span=(7, 11), match='this'>
print(a.span()) # (7, 11)
print(a.start()) # 7
print(a.end()) # 11

pattern = re.compile("this")

b = pattern.search(string)

print(b)
# <re.Match object; span=(7, 11), match='this'>

c = pattern.findall(string)
print(c)    # ['this', 'this']

d = pattern.match(string)
print(d)    # None


string2 = "This is a text"
pattern2 = re.compile("This")

e = pattern2.match(string2)
print(e)
# <re.Match object; span=(0, 4), match='This'>

f = pattern2.fullmatch(string2)
print(f)    # None


# https://regex101.com/
# https://www.w3schools.com/python/python_regex.asp
# https://www.programiz.com/python-programming/regex
INFO