On this page
re.search
Prev: re.RegexFlag | Up: re | Next: re.split
Rel: re.compile, re.error, re.escape, re.findall
Ref: docs.python.org — re.search
Tags: #public #python3 #stdlib #re
Import
import re
from re import search
Signature & Overview
re.search(pattern, string, flags=0)
Scan through string looking for a match to the pattern, returning a Match object, or None if no match was found.
Usage
import re
m = re.search(r"v(\d+\.\d+)", "release v2.5 ready")
assert m is not None and m.group(1) == "2.5"
Gotchas
- Finds only the first match: Use
re.findall()orre.finditer()to get all matches. - Prefer
search()overmatch(): Most developers expecting "does this pattern appear in the string?" wantre.search(), notre.match().
REPL Inspect
from re import search
help(search)
Linked from
Related notes
- tuts/python3/stdlib_module/re/RegexFlag Linked here; links to this page; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/split Linked here; links to this page; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/re/compile Linked here; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/error Linked here; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/escape Linked here; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/findall Linked here; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/finditer shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/fullmatch shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/match shared tags: #re, #stdlib