I'm working on in a chatting group and I want to grab either the number or the name of the user who are sending the message.
I created a pattern to grab the phone number and I called patternnumber
. I don't know how to use logical operators with patterns.
re.search(r'[a-zA-Z]{3,}','11/09/2017 21:46 - Robert: Hello world\n')
<re.Match object; span=(19, 25), match='Robert'>
However
re.search(patternnumber or r'[a-zA-Z]{3,}','11/09/2017 21:46 - Robert: Hello world\n')
Returns me nothing.
I've already tried to use |
instead of 'or' but it gives an error. How can I solve this?
EDIT:
Following one of the answers below which solved my first problem partially, see:
re.search(r'patternnumber|[a-zA-Z]{3,}','11/09/2017 21:46 - Robert: Hello world\n')
<re.Match object; span=(19, 25), match='Robert'>
Okay, however when I add a phone number instead of a person's name, I have another problem:
patternnumber = r'(\(|\+)*\d{1,3}\)* \(*\d{2,5}\)* \d{2,7}((\-|\s)\d{2,5})*((\-|\s)\d{2,5})*'
re.search(r'patternnumber|[a-zA-Z]+','13/09/2017 22:10 - +55 85 8507-4394: Hello world\n')
<re.Match object; span=(37, 42), match='Hello'>
re.search(r'(\(|\+)*\d{1,3}\)* \(*\d{2,5}\)* \d{2,7}((\-|\s)\d{2,5})*((\-|\s)\d{2,5})*|[a-zA-Z]+','13/09/2017 22:10 - +99 99 9999-9999: Hello world\n')
<re.Match object; span=(19, 35), match='+99 99 9999-9999'>
Which should be the answer I expected.
I don't know what is happening, the pattern are the same, the difference is because one I created a variable before and the other I called it directly: