Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

perl

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.

like image 469
Jonathan Swartz Avatar asked Jan 03 '11 23:01

Jonathan Swartz


2 Answers

/(?!)/

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

like image 195
Hugmeir Avatar answered Oct 03 '22 11:10

Hugmeir


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.

like image 22
moinudin Avatar answered Oct 03 '22 12:10

moinudin