On Wed, Dec 1, 2021 at 7:35 PM Abdulla Al Kathiri
<alkathiri.abdu...@gmail.com> wrote:
>
>
> Sometimes when I do complicated regular expressions, I like to divide the 
> patterns from a big pattern to small patterns until I reach what I want. re 
> module functions take the text as a second argument. Furthermore, you can’t 
> pass the Match object directly, you need to extract the group/text from it. 
> If we have a placeholder for the passed argument, we could do both 1) put it 
> wherever we want, not necessarily first 2) further manipulate it so it 
> becomes a valid argument
>
> number = text -> re.search(pattern1, _)
>     -> re.search(pattern2, _.group(0))
>     -> re.search(pattern3, _.group(1))
>     -> float(_.group(0))
>
> Yeah that can look ugly I agree (here intentionally the placeholder is 
> overused). But the person can still write a flowing chained operation if they 
> want or if it’s easier for them to understand. Or just do it the traditional 
> way …
>
> text1 = re.search(pattern1, text).group(0)
> text2 = re.search(pattern2, text1).group(1)
> text3 = re.search(pattern3, text2).group(0)
> number = float(text3)
>

Yep, that would definitely be the cleaner way to do it. Not everything
feels like a pipeline.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/KYOIUKMBUCTHXSD4X7D3B22OKCDDMGTP/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to