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
#1
seeking simple|clean|pythonic way (expression | pattern) to capture {1,} numeric clusters (i.e. "\d+") …
… associated with a single "key_phrase"?

(I am having great difficulty with finding proper search terms for any posted examples.)

goal:
  • 'Match 1 0-c "key_phrase [22 1 333 ...]"'
  • 'Group <int> 12-14 "22"'
  • 'Group <int> 15-16 "1"'
  • 'Group <int> 17-20 "333"'
  • 'Group <int> a-b "\d+"'
    • where:
      • 20 < a <= b < c == b + 1

using (for testing purposes):
  • 'Python 2.7 "flavor"' at regex101
  • test_string = 'key_phrase [22 1 333]'
attempts (with partial success):
  • regex = 'r"key_phrase \[(\d+)+(?: |\])"gm'
    • results:
      • 'Match 1 0-15 "key_phrase [22 "'
      • 'Group 1 12-14 "22"'
  • regex = 'r"key_phrase \[(?:(?: )*(\d+))+\]"gm'
    • results:
      • 'Match 1 0-21 "key_phrase [22 1 333]"'
      • 'Group 1 17-20 "333"'
  • regex = 'r"key_phrase \[((?:(?: )*(?:\d+))+)\]"gm'
    • results:
      • 'Match 1 0-21 "key_phrase [22 1 333]"'
      • 'Group 1 12-20 "22 1 333"'
attempt (with complicated|messy|unpythonic, but successful?, result):
  • regex = 'r"key_phrase \[|(?: )*(\d+)|\]"gm'
    • results:
      • 'Match 1 0-12 "key_phrase ["'
      • 'Match 2 12-14 "22"'
      • 'Group 1 12-14 "22"'
      • 'Match 3 14-16 " 1"'
      • 'Group 1 15-16 "2"'
      • 'Match 4 16-20 " 333"'
      • 'Group 1 17-20 "333"'
      • 'Match 5 20-21 "]"'

Notes:
  • neither "(?:(?<=\[| )(\d+))+" nor "(\?:(\d+)(?= |\])+" are satisfactory as capture-group expressions.
  • '{1,} numeric clusters' is my way of stating :
    • want to ignore any "key_phrase []" potential matches, while capturing an unknown number of \d+ integers
    • (i.e. there could be only one or a multiple number - up to, at least, as many as can fit on a single line - \d+ groups).
  • the '22 1 333' in test_string was selected as a sample of what MIGHT be encountered.
Reply


Messages In This Thread
seeking simple|clean|pythonic way to capture {1,} numeric clusters - by NetPCDoc - Jun-05-2021, 11:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  help seeking for prediction of fixed effect model gotodockforevildragon 1 196 Jun-23-2024, 08:09 AM
Last Post: johnthomson112
  Seeking advice on dask distributed sawtooth500 4 548 Apr-15-2024, 11:07 PM
Last Post: sawtooth500
  Numeric Enigma Machine idev 9 977 Mar-29-2024, 06:15 PM
Last Post: idev
  Can i clean this code ? BSDevo 8 1,151 Oct-28-2023, 05:50 PM
Last Post: BSDevo
  Clean Up Script rotw121 2 1,108 May-25-2022, 03:24 PM
Last Post: rotw121
  How to clean UART string Joni_Engr 4 2,661 Dec-03-2021, 05:58 PM
Last Post: deanhystad
Question Numeric Anagrams - Count Occurances monty024 2 1,635 Nov-13-2021, 05:05 PM
Last Post: monty024
  How to get datetime from numeric format field klllmmm 3 2,115 Nov-06-2021, 03:26 PM
Last Post: snippsat
  Extract continuous numeric characters from a string in Python Robotguy 2 2,781 Jan-16-2021, 12:44 AM
Last Post: snippsat
  Clusters dawid294 6 2,507 Sep-17-2020, 02:18 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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