开发者

A simple Perl regex guaranteed to never match a string? [duplicate]

开发者 https://www.devze.com 2023-02-02 21:32 出处:网络
This question already has 开发者_如何学Goanswers here: Closed 10 years ago. Possible Duplicate: A Regex that will never be matched by anything
This question already has 开发者_如何学Goanswers here: Closed 10 years ago.

Possible Duplicate:

A Regex that will never be matched by anything

I have a script that takes a regex as a parameter. By default I want to set the regex to something that will never match any string, so I can simply say

if ($str =~ $regex)

without e.g. having to check defined($regex) first.

I came up with

qr/[^\s\S]/

but don't know if this will match some utf8 character that is neither a space nor a non-space.


/(?!)/

http://perl.plover.com/yak/regex/samples/slide049.html


Combine a negative lookahead for an arbitrary character followed by a match for that character, e.g.

/(?!x)x/

Works on all the test cases I threw at it. Here are some tests on rubular.


/ ^/ seems to do, and is short(est).

0

精彩评论

暂无评论...
验证码 换一张
取 消