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:
using (for testing purposes):
Notes:
… 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
- where:
using (for testing purposes):
- 'Python 2.7 "flavor"' at regex101
- test_string = 'key_phrase [22 1 333]'
- regex = 'r"key_phrase \[(\d+)+(?: |\])"gm'
- results:
- 'Match 1 0-15 "key_phrase [22 "'
- 'Group 1 12-14 "22"'
- results:
- regex = 'r"key_phrase \[(?:(?: )*(\d+))+\]"gm'
- results:
- 'Match 1 0-21 "key_phrase [22 1 333]"'
- 'Group 1 17-20 "333"'
- results:
- regex = 'r"key_phrase \[((?:(?: )*(?:\d+))+)\]"gm'
- results:
- 'Match 1 0-21 "key_phrase [22 1 333]"'
- 'Group 1 12-20 "22 1 333"'
- results:
- 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 "]"'
- results:
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.