You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by su...@tcs.com on 2004/07/16 07:21:26 UTC

regular expression




Hi,
      I want to validate an attribute, the rule is that the input should
not start with 'Test',
 how to write the regular expression for this validation. Please help me
guys.

Thanks and regards
Subramaniam Olaganthan
Tata Consultancy Services
Mailto: subramaniam.o@tcs.com
Website: http://www.tcs.com


Re: regular expression

Posted by Xavier Noria <fx...@hashref.com>.
On Jul 16, 2004, at 7:21, subramaniam.o@tcs.com wrote:

>       I want to validate an attribute, the rule is that the input 
> should
> not start with 'Test',
>  how to write the regular expression for this validation. Please help 
> me
> guys.

If you validate by code you can negate that it does start that way:

     // pseudocode
     if (!(attr matches ^Test\b)) { ... }

As you surely know, the \b asserts that the word finishes there, so we 
don't match "Testosterone".

In a config file one cannot put that ! operator. In that case use a 
negative look-ahead assertion:

     # in config file
     regexp = ^(?!Test\b)

     // in Java, unconditionally test for a match
     if (attr matches regexp) { ... }

The (?!...) syntax is Perl's. I don't remember if Java libraries use 
the same one though is likely, you could double-check it ("negative 
look-ahead assertion" is the keyword to search for).

There's a more convoluted way to do that if one does not have 
assertions:

      # untested, but you get the idea
      regexp = "^([^T]|T([^e]|e([^s]|s([^t]|t\B))))

But as you see assertions are more concise and readable for that.

-- fxn


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org