4 ms·
Basically the same advice, from the year 2000: http://www.perlmonks.org/?node_id=24640 http://www.perlmonks.org/?node_id=24640 There is one legitimate use case
by perlgeek 12y ago
Basically the same advice, from the year 2000: http://www.perlmonks.org/?node_id=24640 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.
- Mithaldu 12y agoBe aware though that \d will also find arabic or roman numerals. :)
- mhaymo 12y agoRoman 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®ex=\d%2B&input=10%0Ax%0AX%0AVII%0AMMXIV http://regexpal.com/?flags=g®ex=\d%2B&input=10%0Ax%0AX%0A...
- mmastrac 12y agoThere's a whole lot of these numeral characters in unicode, for example: http://www.charbase.com/2169-unicode-roman-numeral-ten http://www.charbase.com/2169-unicode-roman-numeral-ten (even more in http://www.charbase.com/block/number-forms 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}.
- fennecfoxen 12y ago> /\d/.test("\u2169") false
- pyre 12y agoThere's also the character classes in Unicode: http://www.fileformat.info/info/unicode/category/index.htm http://www.fileformat.info/info/unicode/category/index.htm
- jaredmcateer 12y agoRuby also does not match roman numerals with \d http://rubular.com/r/bOCmLIfKdZ http://rubular.com/r/bOCmLIfKdZ
- UweSchmidt 12y ago\d finds arabic numerals? As in 0-9? Scandalous :) http://en.wikipedia.org/wiki/Arabic_numerals http://en.wikipedia.org/wiki/Arabic_numerals
- extra88 12y agoPerhaps they mean Eastern Arabic numerals. http://en.wikipedia.org/wiki/Eastern_Arabic_numerals http://en.wikipedia.org/wiki/Eastern_Arabic_numerals
- acdha 12y agoIndeed - 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 http://en.wikipedia.org/wiki/Numerals_in_Unicode
- greglindahl 12y agoPerl does, by default. There's a modifier if you want to only match ASCII digits.
- eCa 12y agoA somewhat clearer way (imho) is /(\d)\D*$/ since it anchors to the end of the string.
- Rynant 12y agoThis is not the same as finding the last match though. The parent's example will match '2' in '1 of 2 steps.'
- abuzzooz 12y agoActually, both will match the '2' in '1 of 2 steps.'
- ronaldx 12y agoOn 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'.
- Rynant 12y agoThanks, 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*?$/
- icambron 12y agoNo, 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?
- sanderjd 12y agoAlso, if you want to capture everything after some prefix. For instance, in a log: /(WARN|ERR) (.*)/