0

I have following text

txt = 'Lithium 0.25 (7/11/77).  LFTS wnl.  Urine tox neg.  Serum tox + fluoxetine 500; otherwise neg.  TSH 3.28.  BUN/Cr: 16/0.83.  Lipids unremarkable.  B12 363, Folate >20.  CBC: 4.9/36/308 Pertinent Medical Review of Systems Constitutional:'

I want to get date in above expression and i have written following expression.

re.findall(r'(?:[\d{1,2}]+)(?:[/-]\d{0,}[/-]\d{2,4})', txt)

If I execute above expression following output is shown

['7/11/77', '9/36/308']

I don't want "4.9/36/308" this to be included how do I have to change regular expression for this.

Kindly help.

4
  • 1
    I don't know about you but 7/11/77 isn't a date either. Commented Oct 3, 2017 at 9:56
  • stackoverflow.com/questions/38579725/… ? Commented Oct 3, 2017 at 9:56
  • 7/11/77 I consider as a date because according to my requirement if last number is two digits append 19 before it to make a year. Thanks I forgot to mentioned in question Commented Oct 3, 2017 at 10:06
  • @cᴏʟᴅsᴘᴇᴇᴅ Maybe I missed something, but why not? Commented Oct 3, 2017 at 12:19

1 Answer 1

1

You may fix the current regex as

\b(?<!\.)\d{1,2}[/-]\d+[/-]\d{2,4}\b

See the regex demo

The \b will match a word boundary and (?<!\.) negative lookbehind will fail the match if there is a . before the first digit matched.

See the Python demo.

Note that you will have to use a non-regex method later if you need to only get a list of valid dates.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.