Is there a way to print each item in a pattern created with re.compile?

Question:

I’d like to be able to double check that I have compiled my pattern properly by printing each element after compiling.

I tried this:

import re
regex = re.compile('[A-z]')
for l in regex:   # this line raises the TypeError
    print(l)

But naturally I get following error:

TypeError: ‘re.Pattern’ object is not iterable

Is there a way to print following letters?

A
B
C
Asked By: Sabatino Ognibene

||

Answers:

To elaborate Marco Bonelli’s comment:

Short answer: No

There is no programmatic way in Python to represent the compiled pattern like a character-range [A-C] in its expanded form, e.g. as a character-set like [ABC] or as its elements 'A', 'B', 'C'.

When patterns are interpreted and evaluated

The given pattern is a (regular) expression (regex).
In most regex-engines the regex string is compiled to a pattern-object. When applying this to a string we match it. During matching the regex is interpreted by the language, like here Python’s re module’s source.

More on regex elements

Character-classes or -sets like [A-Z] are evaluated first during matching against a given string.

Note:

  • The notation of most regular-expression elements follows a common interpretation standard. So [A-Z] will be interpreted by almost any regex-engine as character-set containing all uppercase letters of the Latin alphabet.
  • Some regex elements are specific to certain regex-engines, we call this flavor

See also:

Answered By: hc_dev
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.