Skip to Content
Functional Python Programming - Second Edition
book

Functional Python Programming - Second Edition

by Steven F. Lott
April 2018
Intermediate to advanced content levelIntermediate to advanced
408 pages
10h 42m
English
Packt Publishing
Content preview from Functional Python Programming - Second Edition

Finding a matching pattern

This technique of creating a collection of multiple conditions can also be applied when using regular expressions. Recall that a pattern's match() or search() method either returns a match object or None. We can leverage this with programs, as shown in the following code:

import rep1 = re.compile(r"(some) pattern")p2 = re.compile(r"a (different) pattern")from typing import Optional, Matchdef matcher(text: str) -> Optional[Match[str]]:    patterns = [p1, p2]    matching = (p.search(text) for p in patterns)    try:       good = next(filter(None, matching))       return good    except StopIteration:       pass

We've defined two patterns that we'd like to apply against a given block of text. Each pattern has a sub-pattern marked with (), which will ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Functional Python Programming - Third Edition

Functional Python Programming - Third Edition

Steven F. Lott

Publisher Resources

ISBN: 9781788627061Supplemental Content