Answers for "python re ?"

18

python regular expression

import re

# The string you want to find a pattern within
test_string = 'Hello greppers!'

# Creating a regular expression pattern
# This is a simple one which finds "Hello"
pattern = re.compile(r'Hello')

# This locates and returns all the occurences of the pattern
# within the test_string
match = pattern.finditer(test_string)

# Outputs all the ocurrences which were returned as 
# as match objects
for match in matches:
  print(match)
Posted by: Guest on February-25-2020
3

re.match() python

import re

pattern = '^a...s$'
test_string = 'abyss'
result = re.match(pattern, test_string)

if result:
  print("Search successful.")
else:
  print("Search unsuccessful.")
Posted by: Guest on July-17-2020
1

python re

if pattern := re.search("[iI] am (.*)", "I am tired"):
    print(f"Hi {pattern.group(1)}, I'm dad!")
Posted by: Guest on January-24-2021
-1

python re

m = re.search(r'[cbm]at', 'aat')
print(m)
Posted by: Guest on December-21-2020

Python Answers by Framework

Browse Popular Code Answers by Language