News for hackers. Stuff that matters.
Re:Why Using .* in Regular Expressions Is Almost Never What ... by perlgeek on Wednesday June 04, 2014 @09:20AM (#7845352)
Basically the same advice, from the year 2000: http://www.perlmonks.org/?node_id=24640

There is one legitimate use case of .* though: advancing to the last match of something. If you want to find the last digit in a string, /.*(\d)/ will readily find it for you.

Re:Why Using .* in Regular Expressions Is Almost Never What ... by sanderjd on Wednesday June 04, 2014 @09:56AM (#7845526)
Also, if you want to capture everything after some prefix. For instance, in a log: /(WARN|ERR) (.*)/
Re:Why Using .* in Regular Expressions Is Almost Never What ... by eCa on Wednesday June 04, 2014 @09:51AM (#7845500)
A somewhat clearer way (imho) is /(\d)\D*$/ since it anchors to the end of the string.
Re:Why Using .* in Regular Expressions Is Almost Never What ... by claar on Wednesday June 04, 2014 @12:42PM (#7846602)
According to the debugger at http://regex101.com/:

  /.*(\d)/
- Searches right-to-left, backtracking until it finds the match.

  /(\d)\D*$/
- Searches left-to-right, going forward a step, backtrack, forward, backtrack, until it finds a match.

If you're looking for a match toward the end of a string, the .* version will be faster.

Re:Why Using .* in Regular Expressions Is Almost Never What ... by hamburglar on Wednesday June 04, 2014 @01:32PM (#7846932)
Wow, regex101.com is very nice, but I never would have noticed that debugger pane if I hadn't gone looking for it after your comment. Incredibly useful tool.
Re:Why Using .* in Regular Expressions Is Almost Never What ... by Rynant on Wednesday June 04, 2014 @10:03AM (#7845573)
This is not the same as finding the last match though. The parent's example will match '2' in '1 of 2 steps.'
Re:Why Using .* in Regular Expressions Is Almost Never What ... by ronaldx on Wednesday June 04, 2014 @10:27AM (#7845694)
On the contrary, it does gives the same result.

$ anchors to the end of the string, \D clears the non-digits from the end to allow \d to match the digit '2'.

Re:Why Using .* in Regular Expressions Is Almost Never What ... by Rynant on Wednesday June 04, 2014 @11:00AM (#7845909)
Thanks, I see where I was wrong now.

In this case when finding the last match from the end, would the lazy quantifier reduce backtracking? e.g. /(\d)\D*?$/

Re:Why Using .* in Regular Expressions Is Almost Never What ... by icambron on Wednesday June 04, 2014 @01:10PM (#7846770)
No, that would work very similarly to the greedy version. The backtracking happens because the \d gets matched to the '1' and the whole thing has to be rolled back when the $ attempts match and instead finds '2' (this would happen again if there were more digits for \d to speculatively match on). So the backtracking is not caused by the laziness or greediness of the \D* ; we really do want to gobble up all of the non-digits.

On the two options generally:

    /(\d)\D*$/
is problematic if you have a lot of digits, while

    /.*(\d)/ 
is problematic if you have a lot of text after the last digit. Both could potentially be optimized by the engine to run right-to-left (the former because it's anchored to the end and the latter because it greedily matches to the beginning), and then both would do well. I'm not sure if that happens in practice.

Overall, I prefer the latter, both because I think it's clearer and because its perf characteristics hold up under a wider variety of inputs.

Edit: how do you make literal asterisks on HN without having a space after them?

Re:Why Using .* in Regular Expressions Is Almost Never What ... by nkozyra on Wednesday June 04, 2014 @01:38PM (#7846979)
In addition to the other explanation, the lazy qualifier is redundant here anyway since there should only be one $ in any given expression.
Re:Why Using .* in Regular Expressions Is Almost Never What ... by chinpokomon on Wednesday June 04, 2014 @10:36AM (#7845756)
As others before me have said, this pattern works as expected. Putting it through regexper, you can visually see this.

http://www.regexper.com/#%2F(%5Cd)%5CD*%24%2F

Re:Why Using .* in Regular Expressions Is Almost Never What ... by ygra on Wednesday June 04, 2014 @10:46AM (#7845816)
Technically, while they both capture the same digit, the match itself it different, including either everything before that digit or everything after it. But I tend to liberally use lookaround to keep the actual match clean myself; maybe others go more often for a capturing group. (Well, and not being able to use arbitrary-length lookaround in most engines might be a reason too.)
Re:Why Using .* in Regular Expressions Is Almost Never What ... by abuzzooz on Wednesday June 04, 2014 @10:27AM (#7845692)
Actually, both will match the '2' in '1 of 2 steps.'
Re:Why Using .* in Regular Expressions Is Almost Never What ... by perlgeek on Wednesday June 04, 2014 @03:47PM (#7847718)
Yes, but that's harder to do for more complicated regexes, because you need to negate a regex (here \d => \D) for this trick.

If you have a complicated regex $r, you can only negate it with (?:(?!$r).), and in that case, .$r is much easier to read :-)

Re:Why Using .* in Regular Expressions Is Almost Never What ... by Mithaldu on Wednesday June 04, 2014 @09:34AM (#7845409)
Be aware though that \d will also find arabic or roman numerals. :)
Re:Why Using .* in Regular Expressions Is Almost Never What ... by mhaymo on Wednesday June 04, 2014 @09:57AM (#7845533)
Roman numerals? I have never encountered a regex implementation where '\d' matches 'X'. It's certainly not the case for Javascript: http://regexpal.com/?flags=g&regex=\d%2B&input=10%0Ax%0AX%0A...
Re:Why Using .* in Regular Expressions Is Almost Never What ... by mmastrac on Wednesday June 04, 2014 @10:06AM (#7845590)
There's a whole lot of these numeral characters in unicode, for example:

http://www.charbase.com/2169-unicode-roman-numeral-ten

(even more in http://www.charbase.com/block/number-forms)

I'm not sure if Javascript matches these in its \d pattern, however, but I think that most regexp engines default to the ascii [0-9] unless you are using \p{Number}.

Re:Why Using .* in Regular Expressions Is Almost Never What ... by pyre on Wednesday June 04, 2014 @11:06AM (#7845953)
There's also the character classes in Unicode:

http://www.fileformat.info/info/unicode/category/index.htm

Re:Why Using .* in Regular Expressions Is Almost Never What ... by fennecfoxen on Wednesday June 04, 2014 @10:55AM (#7845862)
> /\d/.test("\u2169")

false

Re:Why Using .* in Regular Expressions Is Almost Never What ... by jaredmcateer on Wednesday June 04, 2014 @10:59AM (#7845896)
Ruby also does not match roman numerals with \d

http://rubular.com/r/bOCmLIfKdZ

Re:Why Using .* in Regular Expressions Is Almost Never What ... by UweSchmidt on Wednesday June 04, 2014 @10:33AM (#7845728)
\d finds arabic numerals? As in 0-9? Scandalous :)

http://en.wikipedia.org/wiki/Arabic_numerals

Re:Why Using .* in Regular Expressions Is Almost Never What ... by extra88 on Wednesday June 04, 2014 @11:56AM (#7846315)
Perhaps they mean Eastern Arabic numerals. http://en.wikipedia.org/wiki/Eastern_Arabic_numerals
Re:Why Using .* in Regular Expressions Is Almost Never What ... by acdha on Wednesday June 04, 2014 @02:51PM (#7847388)
Indeed - those are in use by millions of people. I wonder if any regexp implementation has started matching all of the other number symbols in Unicode:

http://en.wikipedia.org/wiki/Numerals_in_Unicode

Re:Why Using .* in Regular Expressions Is Almost Never What ... by greglindahl on Wednesday June 04, 2014 @03:52PM (#7847749)
Perl does, by default.

There's a modifier if you want to only match ASCII digits.