1

I'm usually pretty decent at regex but this basic one has me stumped.

  preg_match('/^(\+|-)\s+(month|week|year|day)$/i', '-1 month');

This is returning 0. Can anyone tell me why? It seems to be breaking on the \s.

Note: I have tried escaping the -.

4
  • Doh, nvm. I missed the numbers. Commented Apr 28, 2015 at 15:50
  • 2
    I would say: ^ That screams for a coffee break ? :) Commented Apr 28, 2015 at 15:51
  • @Rizier123 Indeed. Exactly what I am going to do. :) Commented Apr 28, 2015 at 15:54
  • 1
    When this happens to me... I just go to bed and solve the problem the next day. doesn't matter if it's 11:55AM. Go to bed! Commented Apr 28, 2015 at 15:55

1 Answer 1

3

You didn't account for digits:

preg_match('/^(\+|-)\d+\s+(month|week|year|day)$/i', '-1 month');
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.