0

I would like to write a regex in Python to pull out '28 june 1994' from the string below and convert june to 6:

fur = "missed intake office visit on 28 june 1994 at sierra vista nursing homesuicidal behavior hx of suicidal be"

I have tried:

fur.extract(r'(?P<day>\d?\d)\s(?P<month>\W+)\s(?P<year>\d+)')

june can take multiple forms including: 'jun', 'june', 'june,' 'june;' ect.

1
  • \W+ will match all non-words characters, I'm sure you meant \w+?. Also you want to replace only june or other months too? Commented Sep 1, 2017 at 22:09

3 Answers 3

1

You can use this regex:

(\d{2})[\s]([a-zA-Z]+)[\s](\d{4})

This will output of three groups:

first one: is the day.

second one: is the month name.

third one: is the year

output will be:

Full match  30-42   `28 june 1994`
Group 1.    30-32   `28`
Group 2.    33-37   `june`
Group 3.    38-42   `1994`
Sign up to request clarification or add additional context in comments.

Comments

0

You can try this:

import re

fur = "missed intake office visit on 28 june 1994 at sierra vista nursing homesuicidal behavior hx of suicidal be"

data = re.search("(?P<day>\d{1,})\s(?P<month>[a-zA-Z]+)\s(?P<year>\d{4})", fur)
print(data.groupdict())

Output:

{'month': 'june', 'day': '28', 'year': '1994'}

Comments

0

You need to change only June or other monthes too? If U need to change only June, i guess it is enough

re.sub("\d{1,}\sjun[\,e\;]?\s\d{4}","6",yourstring)

3 Comments

He said that june can take multiple forms, so it's not a valid option.
what about this one?
or this one re.sub("\\b(jun[\,e\;]?)\\b,"6",yourstring)

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.