python-regexPython regex example
Python supports regular expressions through the re
module. Regular expressions are a powerful language for matching text patterns.
Example code
import re
# Match a literal string
result = re.search(r"Hello World", "Hello World")
# Print the result
print(result)
Output example
<re.Match object; span=(0, 11), match='Hello World'>
Code explanation
import re
: imports there
module which provides support for regular expressionsre.search(r"Hello World", "Hello World")
: searches for the literal stringHello World
in the stringHello World
print(result)
: prints the result of the search
Helpful links
More of Python Regex
- How to match a UUID using Python regex?
- How to replace all using Python regex?
- How to match a year with Python Regex?
- How to match whitespace in Python regex?
- How to remove numbers from a string using Python regex?
- How to match an IP address with regex in Python?
- How to get all matches from a regex in Python?
- How to match the beginning of a line with Python regex?
- How to match a YYYY-MM-DD date with Python Regex?
- How to use word boundaries in Python Regex?
See more codes...