0

Let's say we have a string:

John Smith has a birthday of 2/29/90

I'm using python re.search, which should just be normal regex. I DO NOT want the middle part. I only want the name and the date returned. Why does mine not work?

(?=(\(\w+\)))(?=([A-z]{3} (\d{1,2}) \d{2}:\d{2}:\d{2}))(\1)(\2)

EDIT: see verified answer and comment

  • Can you show your code as a [mcve]? Are you using a raw string literal for the regex? Also, what defines a ["name"](https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/) here exactly? The regex shown seems to have no relation to the date string shown (lookaheads, colon delimiters, backreferences...). Thanks. – ggorlen Nov 10 '20 at 23:47
  • The OP has presented a test string, a desired result, a pattern and a question why it does not work. Voted to reopen it. – The fourth bird Nov 12 '20 at 10:50

1 Answers1

2

The string does not contain ( or ) so this \(\w+\) will not match.

The divider for the date part is / and not :

For a match, you don't need the positive lookaheads with backreferences to match it.

You can use 2 capturing groups instead.

Assuming the part of the name starts with uppercase chars as in the example:

([A-Z]\w+(?: [A-Z]\w*)*).*?(\d{1,2}/\d{1,2}/\d{2})

Regex demo

Or if the first 2 words are always the name

(\w+ \w{3,}).*?(\d{1,2}/\d{1,2}/\d{2})

Regex demo

Note that [A-z] is not the same as [A-Za-z]

The fourth bird
  • 154,723
  • 16
  • 55
  • 70