Zero Length Regular Expression (2010)
susam.in
susam.in
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" =~ //'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)
Command line code:1:
int(1)
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.
"plonk".match(RegExp(""))
> Array [ "" ] >> "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/Refe...
I am using Firefox. Thanks for the tip, it's been a little while since I've had to do any work with browsers.
Thank you so much Python. No further questions.
>>> import re; re.compile('').search('foo')
<_sre.SRE_Match object; span=(0, 0), match=''>The article, from 2010, tested python 2.5.