I was trying to write regex for identifying name starting with
Mr.|Mrs.
for example
Mr. A, Mrs. B.
I tried several expressions. These regular expressions were checked on online tool at pythonregex.com. The test string used is:
"hey where is Mr A how are u Mrs. B tt`"
Outputs mentioned are of findall()
function of Python, i.e.
regex.findall(string)
Their respective outputs with regex
are below.
Mr.|Mrs. [a-zA-Z]+ o/p-[u'Mr ', u'Mrs']
why A and B are not appearing with Mr. and Mrs.?
[Mr.|Mrs.]+ [a-zA-Z]+ o/p-[u's Mr', u'. B']
Why s is coming with Mr. instead of A?
I tried many more combinations but these are confusing so here are they. For name part I know regex has to cover more conditions but was starting from basic.
Change your regex like below,
DEMO
Mr\.
,Mrs\.
inside a non-capturing or capturing group , so that the|
(OR) applies to the group itself..
is a special meta character in regex which matches any character except line breaks.OR
Even shorter one,
?
quantifier in the above makes the previous characters
as an optional one.