7 ms·
Zero Length Regular Expression (2010)
- draw_down 5y ago> <_sre.SRE_Match object at 0x7fc6c5a2c510> Thank you so much Python. No further questions.
- formerly_proven 5y agoThis is a fair criticism, though it has been addressed some time in the last twelve years or so >>> import re; re.compile('').search('foo') <_sre.SRE_Match object; span=(0, 0), match=''>
- faho 5y agohttps://bugs.python.org/issue17087 https://bugs.python.org/issue17087, fixed in python 3.4 (released in March 2014). The article, from 2010, tested python 2.5.
- draw_down 5y agoOh nice. Upgrading from Python 2 to 3 is only one major version, should be no sweat!
- asicsp 5y agoIt is `re.Match object` now, which I think is more readable. Also, matched portions can be retrieved using `[N]` in addition to `.group(N)`
- goto11 5y agoAre you praising or complaining that Python returns an object rather than a plain string?
- draw_down 5y ago<_HN.Discussion_Reply object at 0x8eb4c5a2c231>
- lifthrasiir 5y agoIn JavaScript a zero-length regexp `//` coincides with a comment syntax, so it is invalid and alternatives like `/(?:)/` should be used. In my opinion this is the correct approach.
- weavie 5y agoPresumably, you could also do `RegExp("")` to create a zero length regexp. This doesn't seem to match the standard as this site implies it should: "plonk".match(RegExp("")) > Array [ "" ]
- susam 5y agoIt does match. It matches the empty string as your output shows. If there were no matches, the result would have been null instead.[1] >> "foobar".match(RegExp("foo")) Array [ "foo" ] >> "foobar".match(RegExp("bar")) Array [ "bar" ] >> "foobar".match(RegExp("baz")) null The last example above does not result in a match. All three examples below lead to successful matches: >> "foobar".match(RegExp("")) Array [ "" ] >> "".match(RegExp("")) Array [ "" ] >> "plonk".match(RegExp("")) Array [ "" ] I am guessing from the output format in your example that you have used Firefox to present your example. I used Firefox too to present my examples, in order to keep the output format consistent in this thread. If you are indeed using Firefox, you could try expanding the Array result to inspect the property named index in the result. For the first two examples in this comment, the index property would be set to 0 and 3, respectively. For the last three examples (including your "plonk" example), the index property would be set to 0, thus indicating a match at index 0. [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match#return_value https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- weavie 5y agoAh, yes of course! I am using Firefox. Thanks for the tip, it's been a little while since I've had to do any work with browsers.
- tyingq 5y agoInteresting, I hadn't thought much about the zero length case. I'd change the perl example, though, so it's easier to read. Maybe: $ printf "foo\nbar\n" | perl -ne 'print if //' or $ perl -e 'print "yes\n" if "bar" =~ //'
- Sharlin 5y agoSome regex APIs make the distinction between matching and searching. Matching means "is the entire input string accepted by this regular expression", and in this sense the only input that matches the empty regex is the empty string. Searching, on the other hand, means "does the input string contain at least one substring accepted by this regular expression", and the empty string is the substring of every string. One can think of matching as searching with implicit ^ and $ anchors affixed to the regex.
- ruste 5y agoSurprisingly I've found this isn't quite true for match vs. search in python. You would think that searching using a regex starting with a ^ anchor might avoid scanning the whole string but it will do so anyway. It makes a huge performance difference if you miss it.
- bobbylarrybobby 5y agoCould it be looking for a match after every (potential) newline character?
- remram 5y agoOnly if you enable the MULTILINE flag.
- hooby 5y ago$ php -r "var_dump(preg_match('', 'foo'));" PHP Warning: preg_match(): Empty regular expression in Command line code on line 1 PHP Stack trace: PHP 1. {main}() Command line code:0 PHP 2. preg_match($pattern = '', $subject = 'foo') Command line code:1 Command line code:1: bool(false)
- hooby 5y ago$ php -r "var_dump(preg_match('//', 'foo'));" Command line code:1: int(1)
- carl_dr 5y agoRandom hint, unrelated to these two posts, but might of of interest to PHPers : Use single quote around the string you pass to PHP to avoid having to quote variable names. php -r ‘$a=“thing”;’ Doesn’t matter in the parent post’s case, but saves either having to worry about escaping or changing all of the quotes if you subsequently expand your single liner for whatever reason, so a good habit to get into. Same with Perl of course.
- teddyh 5y agoThe “C” section uses the third-party pcre(3) library; I would think that regex(3), the POSIX regular expression functions, would be more appropriate to represent C as a whole.