shalvah January 2017

Regex - Why does the question mark behave like this?

I'm learning regex. When I match this:

\d[^\w]\d

on this

30-01-2003 15:20

I get 3 matches: 0-0, 1-2, 3 5, and 5:2. When I try adding a question mark at the end of the regex (\d[^\w]\d?), my matches don't change. When I move the question mark to after the square bracket (\d[^\w]?\d), the matches are now 30, 01, 20, 03, 15, and 20. When I move the question mark to before the square bracket (\d?[^\w]\d), my matches are the same as in the first case.

Why is this? I know the ? operator marks the preceding character as optional, so I expected the behaviour in the second case, but not in the first or third.

Answers


Edulynch January 2017

U just need

\d+

it will match all numbers

Original Text:

30-01-2003 15:20

Result:

30
01
2003
15
20

Enjoy.

Post Status

Asked in January 2017
Viewed 3,564 times
Voted 12
Answered 1 times

Search




Leave an answer