I am trying to extract numbers and unique identifiers from a string.
text = '+1 (123) 4567, 11111111, some text, 2222 2222, 333-33333-333, 444-444-444-4, some other text, 5555 5555 5555 5555 5, ab12345, xy7890z'
I have these regex:
regex_a = re.findall(r"\d+(?:[\s-]?\d+)*", text)
regex_b = re.findall(r"\(?\+[\d _\-\.\)\(\+]{8,25}[\d]{1}|\d{1,}", text)
regex_c = re.findall(r"^ (?=[a-zA-Z]+|\s+|\$)|(?:(?:[a-zA-Z]{0,}\s?\$?\s*)\d+(?:\.\d+)?)|(?:\d+(?:\.\d+)?\s?[a-zA-Z]{1,})$", text)
combined_regex = re.findall(r"\d+(?:[\s-]?\d+)*|\(?\+[\d _\-\.\)\(\+]{8,25}[\d]{1}|\d{1,}|^ (?=[a-zA-Z]+|\s+|\$)|(?:(?:[a-zA-Z]{0,}\s?\$?\s*)\d+(?:\.\d+)?)|(?:\d+(?:\.\d+)?\s?[a-zA-Z]{1,})$", text)
I intend to combine the 3 regexes into 1 regex. regex_c is used to extract the monetary amount of different combinations in the text. It is working for just that purpose. But I have an issue when combining the 3 regexes together.
Results from combined regex:
['+1 (123) 4567','11111111', '2222', '2222', '333','33333-333', '444,444-444-4', '5555', '5555', '5555', '5555', '5','ab12345','xy7890']
Results from regex_a:
['1', '123', '4567', '11111111', '2222 2222', '333-33333-333', '444-444-444-4', '5555 5555 5555 5555 5', '12345', '7890']
Results from regex_b:
['+1 (123) 4567', '11111111', '2222', '2222', '333', '33333', '333', '444', '444', '444', '4', '5555', '5555', '5555', '5555', '5', '12345', '7890']
Results from regex_c:
['1', '123', ' 4567', '11111111', ' 2222', ' 2222', ' 333', '33333', '333', ' 444', '444', '444', '4', ' 5555', ' 5555', ' 5555', ' 5555', ' 5', 'ab12345', 'xy7890']
The expected output:
['+1 (123) 4567','11111111', '2222 2222', '333-33333-333', '444-444-444-4', '5555 5555 5555 5555 5', 'ab12345', 'xy7890z']
Is there any way that I can combine the 3 regexes into 1 regex to extract the expected results and maintaining the logic for regex_c?