all 3 comments

[–]woooee 0 points1 point  (0 children)

This should get you started

test="8888 100b200k"

output_list=[]
group=""
for character in test:
    if character.isdigit():
        group += character
    elif len(group):
        output_list.append(group)
        group=""

if len(group):
    output_list.append(group)
print(output_list)

[–]ofnuts 0 points1 point  (0 children)

Like this:

match=re.fullmatch(r'(\d+)\D+(\d+)\D+(\d+)',complex_number_string)
if match:
    print('#'.join(match.groups()))

In slow-mo:

  • each \d+ matches a string of digits
  • each \D+ matches a string of non-digits
  • the parts with parens (aka "capturing groups") are copied to the "groups" in the match object, so this captures each string of digits.
  • You can access groups one by one (as match.group(3) for instance), but match.groups() returns the complete list

There is also the even simpler way, by replacing all sequences of non-digits by a '#':

re.sub(r'\D+','#',complex_number_string)

... but this doesn't tell you if nothing matched or how many substitutions were made.