Skip to content

Instantly share code, notes, and snippets.

@xoxwgys56
Created August 4, 2021 05:48
Show Gist options
  • Save xoxwgys56/6faeae94b5432ce1355a0eec9a9e9528 to your computer and use it in GitHub Desktop.
Save xoxwgys56/6faeae94b5432ce1355a0eec9a9e9528 to your computer and use it in GitHub Desktop.
find digit group from text using re
import re
"""
find digit group from text
"""
def print_match(m):
print('\t',{
'start':m.start(),
'group': m.group(),
'span': m.span(),
})
include_digit_text = 'sr_pg_1_11a3222'
print(f'find digit from "{include_digit_text}"')
for m in re.finditer(r'[\d]+', include_digit_text):
print_match(m)
not_include_digit_text = 'sr_pg_'
print(f'find digit from "{not_include_digit_text}"')
for m in re.finditer(r'[\d]+', not_include_digit_text):
print_match(m)
print('finished.')
@xoxwgys56
Copy link
Author

Result

find digit from "sr_pg_1_11a3222"
	 {'start': 6, 'group': '1', 'span': (6, 7)}
	 {'start': 8, 'group': '11', 'span': (8, 10)}
	 {'start': 11, 'group': '3222', 'span': (11, 15)}
find digit from "sr_pg_"
finished.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment