this post was submitted on 23 Jul 2024
31 points (100.0% liked)

Python

7363 readers
9 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

๐Ÿ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

๐Ÿ Python project:
๐Ÿ’“ Python Community:
โœจ Python Ecosystem:
๐ŸŒŒ Fediverse
Communities
Projects
Feeds

founded 2 years ago
MODERATORS
 

I wrote a TUI application to help you practice Python regular expressions. There are more than 100 exercises covering both the builtin re and third-party regex module.

If you have pipx, use pipx install regexexercises to install the app. See the repo for source code and other details.

all 3 comments
sorted by: hot top controversial new old
[โ€“] alyth@lemmy.world 1 points 1 year ago (1 children)

Thanks for sharing this. I took the time to read through the documentation of the re module. Here's my review of the functions.

Useful:

  • re.finditer returns an iterator over all Match objects
  • re.search returns the first Match object or None if there are no matches.
  • r'' use raw strings for patters so you don't have to worry about backslashes
  • the optional flags argument modifies the behaviour (case insensitive, multiline)

Utility:

  • re.sub replace each match in the string
  • re.split split a string by a regular expression

The Match object:

  • match.groups(0) returns the portion of text matched by the pattern
  • match.groups(1) returns the first capturing group
  • match.groups(2) returns the second capturing group, and so on

I don't understand why these exist:

  • re.match like search, but only matches at the beginning of the string. why not just use '^' or '\A' in the pattern you pass to 'search'?
  • re.fullmatch like 'search', but only if the full string matches. Why not just use '\A' and '\Z' in the pattern you pass to 'search'?
  • re.findall Returns all matches. It seems like a shitty version of 'finditer'. The function has three different return types which depend on the pattern you pattern you pass to the function. Who wants to work with that?