## Search for pattern 'bb' in string 'aabbcc'.
## All of the pattern must match, but it may appear anywhere.
## On success, match.group() is matched text.
match = re.search(r'bb', 'aabbcc') # found, match.group() == "bb"
match = re.search(r'cd', 'aabbcc') # not found, match == None
## . = any char but
match = re.search(r'...c', 'aabbcc') # found, match.group() == "abbc"
## d = digit char, w = word char
match = re.search(r'ddd', 'p123g') # found, match.group() == "123"
match = re.search(r'www', '@@abcd!!') # found, match.group() == "abc"
import re
string = "Python is fun"
# check if 'Python' is at the beginning
match = re.search('APython', string)
if match:
print("pattern found inside the string")
else:
print("pattern not found")
# Output: pattern found inside the string
match = re.search(pattern, str)
re.search(pattern, string, flags=0)
# pattern: The first argument is the regular expression pattern we want to search inside the target string.
# string: The second argument is the variable pointing to the target string (In which we want to look for occurrences of the pattern).
# flags: Finally, the third argument is optional and it refers to regex flags by default no flags are applied.
exp = "(d{1,3}.){3}d{1,3}"
ip = "blah blah 192.168.0.185 blah blah"
match = re.search(exp, ip)
print match.group()
if re.search(rf"(?=w){TEXTO}(?!w)", subject, re.IGNORECASE):
...do something
# This is how we use find
# Open a file
hand = open('path and the name of the file')
count1 = 0
for line in hand:
# remove new line (/n) at the end of the line
line = line.rstrip()
# If there is no 'From' it will give a -1
if line.find('Enter the sub string here')>= 0:
count1 += 1
print(count1, line)
# Close the file
hand.close()
# This is how we use re.search()
import re
count2 = 0
hand2 = open('path and the name of the file')
for line2 in hand2:
line2 = line2.rstrip()
if re.search('Enter the sub string here', line2):
count2 += 1
print(count2, line2)
# Close the file
hand2.close()