Re: Correct syntax for pathological re.search()

Liste des GroupesRevenir à cl python 
Sujet : Re: Correct syntax for pathological re.search()
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.python
Date : 12. Oct 2024, 13:59:52
Autres entêtes
Organisation : Stefan Ram
Message-ID : <compiled-20241012123950@ram.dialup.fu-berlin.de>
References : 1 2 3 4 5
"Peter J. Holzer" <hjp-python@hjp.at> wrote or quoted:
But - without having looked at the implementation - it's far from clear
that the compiled form would be useful to the user.

  So, what he might be getting at with "compiled form" is a
  representation that's easy on the eyes for us mere mortals.

  You could, for instance, use colors to show the difference between
  object and meta characters. In that case, the regex "\**" would
  come out as "**", but the first "*" might be navy blue (on a white
  background), so just your run-of-the-mill object character, while
  the second one would be burgundy, flagging it as a meta character.

  So, simplified, that would be something like:

import re
import tkinter as tk
import time

def tokenize_regex( pattern ):
    tokens = []
    i = 0
    while i < len( pattern ):
        if pattern[ i ] == '\':
            if i + 1 < len( pattern ):
                tokens.append( ( 'escaped', pattern[ i+1: i+2 ]))
                i += 2
            else:
                tokens.append( ('error', 'Incomplete escape sequence' ))
                i += 1
        elif pattern[i] == '*':
            tokens.append( ( 'repetition', '*' ))
            i += 1
        else:
            tokens.append( ( 'plain', pattern[ i ]))
            i += 1
   
    return tokens

root = tk.Tk()
root.configure( bg='white' )

regex = r'\**'
result = tokenize_regex( regex )

for token_type, token_value in result:
    if token_type == 'plain' or token_type == 'escaped':
        tk.Label( root, text=token_value, font=( 'Arial', 40 ), fg='#4070FF', bg='white' ).pack( side='left' )
    elif token_type == 'repetition':
        tk.Label( root, text=token_value, font=( 'Arial', 40 ), fg='#C02000', bg='white' ).pack( side='left' )

root.mainloop()

  .



Date Sujet#  Auteur
7 Oct 24 * Correct syntax for pathological re.search()28Michael F. Stemper
7 Oct 24 +* Re: Correct syntax for pathological re.search()6Stefan Ram
7 Oct 24 i`* Re: Correct syntax for pathological re.search()5Michael F. Stemper
7 Oct 24 i `* Re: Correct syntax for pathological re.search()4Stefan Ram
7 Oct 24 i  +- Re: Correct syntax for pathological re.search()1Jon Ribbens
8 Oct 24 i  `* Re: Correct syntax for pathological re.search()2Pieter van Oostrum
9 Oct 24 i   `- Re: Correct syntax for re.search() (Posting On Python-List Prohibited)1Lawrence D'Oliveiro
8 Oct 24 +- Re: Correct syntax for pathological re.search()1Karsten Hilbert
8 Oct 24 +- Re: Correct syntax for pathological re.search()1MRAB
8 Oct 24 +* Re: Correct syntax for pathological re.search()3MRAB
8 Oct 24 i`* Re: Correct syntax for pathological re.search()2Stefan Ram
8 Oct 24 i `- Re: Correct syntax for pathological re.search()1Stefan Ram
8 Oct 24 +* Re: Correct syntax for pathological re.search()4Karsten Hilbert
8 Oct 24 i`* Re: Correct syntax for pathological re.search()3Alan Bawden
9 Oct 24 i +- Re: Correct syntax for pathological re.search()1MRAB
9 Oct 24 i `- Re: Correct syntax for pathological re.search()1Karsten Hilbert
11 Oct 24 +- Re: Correct syntax for pathological re.search()1<avi.e.gross
12 Oct 24 +- Re: Correct syntax for pathological re.search()1MRAB
12 Oct 24 +* Re: Correct syntax for pathological re.search()2Peter J. Holzer
12 Oct 24 i`- Re: Correct syntax for pathological re.search()1Stefan Ram
12 Oct 24 +- Re: Correct syntax for pathological re.search()1Thomas Passin
12 Oct 24 +- Re: Correct syntax for pathological re.search()1<avi.e.gross
12 Oct 24 +- Re: Correct syntax for pathological re.search()1Thomas Passin
13 Oct 24 +- Re: Correct syntax for pathological re.search()1Stefan Ram
18 Oct 24 `* Re: Correct syntax for pathological re.search()4Peter J. Holzer
19 Oct 24  `* Re: Correct syntax for pathological re.search()3jak
21 Oct 24   `* Re: Correct syntax for pathological re.search()2Peter J. Holzer
21 Oct 24    `- Re: Correct syntax for pathological re.search()1Stefan Ram

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal