Python Forum
seeking simple|clean|pythonic way to capture {1,} numeric clusters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
seeking simple|clean|pythonic way to capture {1,} numeric clusters
#3
(Jun-06-2021, 05:55 AM)Gribouillis Wrote: I don't think you can capture a variable number of groups in the same match. Each captured group correspond to a single group in the regex. For example the regex r"key_phrase \[(?:(?: )*(\d+))+\]" has visibly a single capturing group (\d+) hence there will be only a single group in the match.
While you might be technically correct, I expect python to do better than that!

r"key_phrase \[|(?: )*(\d+)|\]" has the same single visible capture group, and while in that brief form does include multiple matches, it also returns multiple instances of Group 1; and relies on the proper placement of the two | "alternative" designators to return ALL the desired captures.

Along side the fact that the expression r"key_phrase \[((?:(?: )*(?:\d+))+)\]" does eliminate the extra matches, while failing to separate out the individual \d+ instances … it seems to me, that if I REALLY knew what I was doing, these two expression contain most of the clues on how to proceed to a simpler | cleaner | more pythonic expression that does achieve the desired goal.

I am somewhat clueless as to how acceptable, pythonic speaking, multiple instances of the same group are? vs the incrementing group number that would result if one took the time to type in some thousand (or so) optional-capture-group-sub-expressions? And I still expect that python should have a short elegant way of coding an expression to make multiple captures in such situations.
Reply


Messages In This Thread
RE: seeking simple|clean|pythonic way to capture {1,} numeric clusters - by NetPCDoc - Jun-06-2021, 09:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Seeking advice on dask distributed sawtooth500 4 499 Apr-15-2024, 11:07 PM
Last Post: sawtooth500
  Numeric Enigma Machine idev 9 901 Mar-29-2024, 06:15 PM
Last Post: idev
  Can i clean this code ? BSDevo 8 1,113 Oct-28-2023, 05:50 PM
Last Post: BSDevo
  Clean Up Script rotw121 2 1,089 May-25-2022, 03:24 PM
Last Post: rotw121
  How to clean UART string Joni_Engr 4 2,638 Dec-03-2021, 05:58 PM
Last Post: deanhystad
Question Numeric Anagrams - Count Occurances monty024 2 1,609 Nov-13-2021, 05:05 PM
Last Post: monty024
  How to get datetime from numeric format field klllmmm 3 2,091 Nov-06-2021, 03:26 PM
Last Post: snippsat
  Extract continuous numeric characters from a string in Python Robotguy 2 2,758 Jan-16-2021, 12:44 AM
Last Post: snippsat
  Clusters dawid294 6 2,499 Sep-17-2020, 02:18 PM
Last Post: Larz60+
  New user seeking help EdRaponi 2 56,469 Jun-23-2020, 12:03 PM
Last Post: EdRaponi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020