How I can check regex-string?

For the Compleat Fan
Locked
alex
Posts: 100
Joined: Thu Mar 10, 2005 2:27 pm
Location: Russia

How I can check regex-string?

Post by alex »

Excuse me for bad English :( ,

I want give to my user possibility enter regular expresson.

How can I check, that the expression is right?

example:

Code: Select all

> (set 'test ".?")
".?"
> (regex test "abcde")
("a" 0 1)
> (set 'test "?")
"?"
> (regex test "abcde")

regular expression in function regex : "offset 0 nothing to repeat:"
>
I want know about
is the "test" right regular expression
before I use (regex)

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

I think you might want to turn it around.. every input is allowed unless you
get an unknown return value, like the error you displayed..

You could build a 'catch around it...

Norman.
-- (define? (Cornflakes))

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

This looks ungainly to me:

Code: Select all

(set 'user-input "ab") 
(set 'test-string "abcde")

(set 'regex-command (string {(} {regex [text]} user-input {[/text] [text]} test-string {[/text])}))
(set 'r (eval-string regex-command 'failed))
If r is 'failed, the regex failed; otherwise it's the result of the regex function.

There's probably a smoother way to do this...

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

You could do something like:

Code: Select all

(or (regex 'user-inputed-pattern 'some-string) (some-action 'in 'the 'event 'that 'regex 'evaluates 'to 'nil))
Or use

Code: Select all

(if (regex...) (do something...) (do something else...)).
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

But isn't Alex's problem that a failed regex stops execution?

Code: Select all

(or 
  (regex ".+?+" "a string") 
  (println "did we get here?"))

regular expression in function regex : "offset 6 nothing to repeat:"

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

Code: Select all

(catch (regex needle haystack))
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Jeff is on the right track, but you have to use the second syntax of 'catch' with the extra parameter symbol to check for error exceptions:

Code: Select all

 (catch (regex ".+?+" "a string") 'result)
If the there was an error the whole expression will return 'nil' and you have the error message in 'result'.

Else if the whole expression returns 'true', there was no error and you can inspect 'result' for the result of the the 'regex' expression, which still may be 'nil' if there was no match. Here is the whole picture:

Code: Select all

(catch (regex ".+?+" "a string") 'result) => nil

result => regular expression in function regex : "offset 3 nothing to repeat:"

(catch (regex "r" "a string") 'result) => true

result => ("r" 4 1)

(catch (regex "x" "a string") 'result) => true

result => nil
Lutz

alex
Posts: 100
Joined: Thu Mar 10, 2005 2:27 pm
Location: Russia

Post by alex »

Very good!
Thanks Your Lutz! :-)

Locked