0

I am new to regex expressions in python and trying to find all occurrences of phone nos in a string wherein the country code with a + is not mandatory

import re
st="it contains std code +91-888-888-9999 and 777-897-4565"

pattern=r"(\+?\d{2}\-)?\d{3}-\d{3}-\d{4}"

c=re.findall(pattern,st)
if c is not None:
    print(c)
else:
    print("it is none")

If i use search it returns only the first match. Using findall it returns a list with the pattern in the bracket matched as the first value and empty as next

['+91-','']

If a change the pattern to

pattern=r"(\+?\d{2}\-)?(\d{3}-\d{3}-\d{4})"

i get list of tuples with country code as the first value and phone no as the second value. Can i get the entire value e.g +91-888-888-9999 as a single unit using search() and findall(). kindly update

user2779311
  • 1,688
  • 4
  • 23
  • 31
  • Your `if c is not None:` is pointless. `re.findall` never returns `None`. – Aran-Fey Oct 28 '18 at 17:02
  • that check i had put when using search. That carried over. It only shows items that we enclose in brackets () and not otherwise. This is not the same as marked duplicate as it is intending to show only () items in pattern whereas our case we need output that is not in the () brackets also in the pattern. – user2779311 Oct 29 '18 at 07:24
  • I don't see how your question is different from the duplicate. That OP has the same problem as you - only the text inside the parentheses () shows up in the result, and they want to change that. – Aran-Fey Oct 29 '18 at 07:26
  • Can you suggest what regex can we use in this case to get the output desired. – user2779311 Oct 29 '18 at 07:29
  • From [Wiktor's answer](https://stackoverflow.com/a/31915134/1222951): *"convert all capturing groups into non-capturing groups"*: `(?:\+?\d{2}\-)?\d{3}-\d{3}-\d{4}` – Aran-Fey Oct 29 '18 at 07:32
  • yes, that is what i am looking for. how can i convert the expression in bracket to one without it – user2779311 Oct 29 '18 at 07:35
  • You can't and you don't need to. – Aran-Fey Oct 29 '18 at 07:35
  • but this is not giving the expected output of the entire phone no with country code as one entry in the return list – user2779311 Oct 29 '18 at 07:49
  • I get `['+91-888-888-9999', '777-897-4565']` as output with that regex. Isn't that what you want? – Aran-Fey Oct 29 '18 at 07:56
  • I am getting [('+91-', '888-888-9999'), ('', '777-897-4565')] on my system with regex (\+?\d{2}\-)?(\d{3}-\d{3}-\d{4}) – user2779311 Oct 29 '18 at 12:26
  • 1
    I even gave you the regex to copy/paste... come on... For the last time: `(?:\+?\d{2}\-)?\d{3}-\d{3}-\d{4}` – Aran-Fey Oct 29 '18 at 12:27
  • I missed out. Thanks – user2779311 Oct 29 '18 at 13:41

0 Answers0