1

This is the regex I am using:

date = "1981-89"
date = re.findall(r'\d+', date)

if the date is 1981-89 this returns [1981],[89]. What do I add to the regex to ignore anything after the dash including the dash itself?

Thanks!

4
  • why not just split the string and take what you need? Commented Apr 6, 2015 at 22:02
  • 2
    date.partition('-')[0] would be a lot more efficient than using Regex. Commented Apr 6, 2015 at 22:02
  • Hi guys, thanks for the response. I am trying to learn regex. I can easily parse the string but I am trying to learn something new. Thanks! Commented Apr 6, 2015 at 22:04
  • re.findall(r'(\d+)-', date) will work. Commented Apr 6, 2015 at 22:09

3 Answers 3

3

If you need to use regular expressions, use the first element of a match search:

re.match(r'(\d+)', date).group() # matching using parenthesis in regex
Sign up to request clarification or add additional context in comments.

2 Comments

I agree with this. It seems to me that English Grad is asking for the first matching item, and thus should be using match or search rather than findall.
@bmhkim Yes I changed from findall to match. Never really understood the difference between match and search.
2

You can remove it with re.sub :

>>> re.sub(r'-.*','',date)
'1981'

Comments

0

What do I add to the regex to ignore anything after the dash including the dash itself?

You can use this regex:

date = "1981-89"
date = re.match(r'\d+(?=-)', date).group()
// 1981

(?=-) is a lookahead that makes sure we are only matching number that is followed by a hyphen.

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.