Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #198072

Re: Pythonic way to combine regular expressions?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups comp.lang.python, alt.folklore.computers
Subject Re: Pythonic way to combine regular expressions?
Date 25 Sep 2026 19:08:54 GMT
Organization Stefan Ram
Lines 61
Expires 1 Sep 2027 11:59:58 GMT
Message-ID <pattern-20260925200755@ram.dialup.fu-berlin.de> (permalink)
References <MrztS.208529$Nn1.7474@fx18.ams4>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de SRZCxEWlVHIlCExSqfRBJAYDm0HNO760y7gTp+zy+qB0vM
Cancel-Lock sha1:lzTjK59QKB8weLUIUrLD5Li+6XQ= sha256:dfp3bj3GbTZx/CctZ+s3idFZjPAc/Q/+hWSoXh7zRAk=
X-Copyright (C) Copyright 2026 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed.
X-No-Archive Yes
Archive no
X-No-Archive-Readme "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access.
X-No-Html yes
Content-Language en
Xref csiph.com comp.lang.python:198072 alt.folklore.computers:235704

Cross-posted to 2 groups.

Show key headers only | View raw


Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> wrote or quoted:
>What is a good way to combine regular expressions in Python?  We are
>cogitating the most /Pythonic/ way to make this happen.
>    number:
>        sign? integer-constant
>        | sign? floating-point-constant
>    integer-constant:
>        digit-sequence
>    floating-point-constant:
>        fractional-constant exponent?
>        | digit-sequence exponent
>    fractional-constant:
>        digit-sequence? "." digit-sequence
>        | digit-sequence "."
>    exponent:
>        ( "e" | "E" ) sign? digit-sequence
>    sign:
>        "+" | "-"
>    digit-sequence:
>        digit
>        | digit digit-sequence
>    digit:
>        "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

  Maybe something like this?

import re
from typing import Final

DIGIT: Final[ str ]= r"[0-9]"

DIGIT_SEQUENCE: Final[ str ]= rf"{DIGIT}+"

SIGN: Final[ str ]= r"[+-]"

EXPONENT: Final[ str ]= rf"[eE]{SIGN}?{DIGIT_SEQUENCE}"

FRACTIONAL_CONSTANT: Final[ str ]= \
rf"(?:(?:{DIGIT_SEQUENCE})?\.{DIGIT_SEQUENCE}|{DIGIT_SEQUENCE}\.)"

INTEGER_CONSTANT: Final[ str ]= DIGIT_SEQUENCE

FLOATING_POINT_CONSTANT: Final[ str ]= \
rf"(?:{FRACTIONAL_CONSTANT}(?:{EXPONENT})?|{DIGIT_SEQUENCE}{EXPONENT})"

NUMBER_PATTERN: Final[ str ]= \
rf"^{SIGN}?(?:{INTEGER_CONSTANT}|{FLOATING_POINT_CONSTANT})$"

NUMBER_VALIDATOR: Final[ re.Pattern[ str ]] = \
re.compile( NUMBER_PATTERN )

example = "123"
is_ok = bool( NUMBER_VALIDATOR.match( example ))
print( is_ok )

  (Actually, the Pythonic pattern uses "(" where I wrote "\".)

Newsgroups: comp.lang.python,alt.folklore.computers
Followup-To: comp.lang.python

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Pythonic way to combine regular expressions? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-09-26 02:54 +0800
  Re: Pythonic way to combine regular expressions? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-25 19:08 +0000
    Re: Pythonic way to combine regular expressions? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-25 19:19 +0000
  Re: Pythonic way to combine regular expressions? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-26 10:04 +0000
    Re: Pythonic way to combine regular expressions? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-09-26 20:09 +0800
      Re: Pythonic way to combine regular expressions? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-26 12:22 +0000

csiph-web