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

Time:09-28

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

CodePudding user response:

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:

  • Related