Check for valid perl regex 
Author Message
 Check for valid perl regex

Is there a routine that will check the validity of a perl regex
instead of having perl puke when you try to use an invalid one?

I'm reading in a set of regular expressions from a file and i want to
check them before actually using them.
--

University of Toronto, Dictionary of Old English Project  utzoo!utdoe!peter



Mon, 07 Feb 1994 07:21:49 GMT  
 Check for valid perl regex

:Is there a routine that will check the validity of a perl regex
:instead of having perl puke when you try to use an invalid one?

:I'm reading in a set of regular expressions from a file and i want to
:check them before actually using them.

Use eval to trap exceptions:

    {
        print "enter regexp: ";
        chop($regexp = <STDIN>);
        eval "m\200${regexp}\200";

        s/at \(eval\).*//;
        warn;
        redo;
    }

Make sure that whatever delim you use in your eval'd m// test you use
later.  \200 is almost always safe, but you can't say m\200 out of an
eval and have it work right.  m^Afoo^A is often good enough.  

--tom



Mon, 07 Feb 1994 13:57:47 GMT  
 Check for valid perl regex

Peter> Is there a routine that will check the validity of a perl regex
Peter> instead of having perl puke when you try to use an invalid one?

Use eval.



--
______________________________________________________________________
 robert earl            /       "Obviously then a Woman is not to be

                        /       where she can turn round."



Mon, 07 Feb 1994 14:25:59 GMT  
 Check for valid perl regex


:
:Peter> Is there a routine that will check the validity of a perl regex
:Peter> instead of having perl puke when you try to use an invalid one?
:
:Use eval.
:


If eval blows up, and all you're doing is a regexp, you don't
really need to do a match, do you?  

Beware of cases like this: what if the used used a / in his pattern?

--tom



Mon, 07 Feb 1994 14:29:47 GMT  
 Check for valid perl regex

Quote:
>    eval "m\200${regexp}\200";

Beware of cases like this: what if there's a \200 character in the
regexp?  It's best to quote occurrences of the delimiter in the
regexp, but only if they're not already quoted.
        $d = "\200";
        $regexp =~ s/(\\\\)*$d/\\$&/g;
I hope I got that right.
--



Tue, 08 Feb 1994 01:42:46 GMT  
 Check for valid perl regex

:>   eval "m\200${regexp}\200";
:
:Beware of cases like this: what if there's a \200 character in the
:regexp?  It's best to quote occurrences of the delimiter in the
:regexp, but only if they're not already quoted.

I don't know why I was making such a deal out of this.  

    eval '/$regexp/';

is fine, even if there are slashes in the regexp.  I must
have picked up unnecessary habits long ago.  I think it might
have been when we were trying to build regexp stuff to be eval'd
to get the faster performance of apparently constant strings, which
is (nearly) no longer necessary.

--tom



Tue, 08 Feb 1994 02:19:31 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Checking if URL is valid/exist using PERL

2. Check for valid hex value

3. Checking if a url is valid

4. How do I check for a valid URL

5. checking if mail address is valid

6. checking for a valid date-time

7. A Script for checking emails valid?

8. Checking for valid Email...

9. check for valid e-mail address

10. How to check if an email address is valid

11. Checking form for required/valid fields

12. Checking for a valid e-mail address

 

 
Powered by phpBB® Forum Software