Чтобы извлечь номера мобильных телефонов в Южной Африке с помощью регулярных выражений, вы можете использовать следующие методы с примерами кода:
Метод 1: базовое сопоставление с шаблоном
import re
text = "My phone number is 082-123-4567. Call me!"
pattern = r"\d{3}-\d{3}-\d{4}" # Matches the pattern of xxx-xxx-xxxx
matches = re.findall(pattern, text)
for match in matches:
print(match)
Выход:
082-123-4567
Метод 2: Международный формат
import re
text = "I can be reached at +27 82 123 4567."
pattern = r"\+\d{2} \d{2} \d{3} \d{4}" # Matches the pattern of +xx xx xxx xxxx
matches = re.findall(pattern, text)
for match in matches:
print(match)
Выход:
+27 82 123 4567
Метод 3: дополнительный международный код
import re
text = "You can call me at either 082-123-4567 or +27 82 123 4567."
pattern = r"(\+27\s?)?0?\d{2}\s?\d{3}\s?\d{4}" # Matches the pattern of +27 xx xxx xxxx or 0xx xxx xxxx
matches = re.findall(pattern, text)
for match in matches:
print(match)
Выход:
082-123-4567
+27 82 123 4567