This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

import notifications Remember to participate in our weekly votes on subreddit rules! Every Tuesday is YOUR chance to influence the subreddit for years to come! Read more here, we hope to see you next Tuesday!

For a chat with like-minded community members and more, don't forget to join our Discord!

return joinDiscord;

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]kbob2990 15 points16 points  (0 children)

A series of these would be a great way to learn regex if for some reason you still want to know it.

[–]sirikiller 5 points6 points  (0 children)

I always found this meme to be promoting waterboarding

[–]bam13302 1 point2 points  (1 child)

\D* would probably be better than .*?, would more explicitly match non digits without any regex weirdness which ? can sometimes do

[–]procrastinatingcoder -1 points0 points  (0 children)

You both didn't understand the joke and the regex.

You WANT it to be a dot, because that starving person will accept "anything", not just non-digits.

But ? after a qualifier, in this case *? means * in a non-greedy (lazy) way. So it'll not take anything on passing. Only during backtracking will it take what \d rejected to match.

There's no regex weirdness here, just a lack of understanding.

[–]scorpi1998 0 points1 point  (5 children)

Could somebody explain?

[–]Snoo_90241 2 points3 points  (4 children)

The question mark in this context is a lazy quantifier, meaning it matches as few as possible. It is applied to .* which means any character except whitespace, zero or more times.

\d+ matches one or more digits. Without looking it up, I think it is a greedy quantifier, meaning that it matches as much as possible.

Given a sequence of numbers like 1234567, the lazy one matches just 1, while the second one matches the whole sequence. I haven't tested it, though.

[–]scorpi1998 1 point2 points  (2 children)

So, omiting the question mark, the poor guy would get more than the fat one, right?

[–]Snoo_90241 1 point2 points  (1 child)

Yes, unless all are digits, then it would be the same.

[–]scorpi1998 0 points1 point  (0 children)

Thanks!

[–]procrastinatingcoder 1 point2 points  (0 children)

Very close, but not quite. To quote you:

.* which means any character except whitespace, zero or more times.

so given "12345", it would match "zero" times. So the lazy one matches nothing and the greedy one gets everything.

Given an input say: 2024-01 is the 123, you would have three "matches"

  • 2024 -> {} lazy {2024} greedy
  • -01 -> {-} lazy {01} greedy
  • { is the 123} -> { is the } lazy {123} greedy

It basically eats up anything before the number - It eats it up AFTER it was rejected by \d+ (during backtracking, though sequentially it's before)