Tech Support > Computers & Technology > Programming > Regular Expression: match only if string does NOT contain a certain character
Regular Expression: match only if string does NOT contain a certain character
Posted by Mike Hnatt on July 27th, 2003


Hello,
Anyone know the Regular Expression that I can use to validate and make sure
a user does not include an apostrphoe or quotation in their input?

I thought something like *[^'^"]* or maybe *^'^"* would work but it doesn't.

Thanks!


Posted by Mike Hnatt on July 27th, 2003


Great! Works perfectly. Thanks Roger!
Mike

"Roger Willcocks" <rkww@rops.org> wrote in message
news:bfv65n$q3$1$8300dec7@news.demon.co.uk...


Posted by John W. Krahn on July 27th, 2003


Mike Hnatt wrote:
You need to test whether the character(s) exist and use the boolean
negation of that test.

if ( $input =~ /['"]/ ) {
# includes an apostrphoe or quotation
}
else {
# DOES NOT include an apostrphoe or quotation
}

Or:

unless ( $input =~ /['"]/ ) {
# DOES NOT include an apostrphoe or quotation
}

Or:

if ( $input !~ /['"]/ ) {
# DOES NOT include an apostrphoe or quotation
}

Or:

if ( !($input =~ /['"]/) ) {
# DOES NOT include an apostrphoe or quotation
}


John
--
use Perl;
program
fulfillment

Posted by Derk Gwen on July 27th, 2003


# > Anyone know the Regular Expression that I can use to validate and make sure
# > a user does not include an apostrphoe or quotation in their input?
# >
# > I thought something like *[^'^"]* or maybe *^'^"* would work but it doesn't.

On most brands of RE, ^[^'"]*$ will match a string with no quotes, and ['"]
(or ^.*['"].*$) will match a string with at least one.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Haven't you ever heard the customer is always right?

Posted by Programmer Dude on July 29th, 2003


[NOTE: Using /RE/ to quote Regular Expressions....
Using {c} to quote individual characters as required...]

Roger Willcocks wrote:

This is not a valid RE. The {*} requires something in front
of it to repeat. If you wrote /.*[^'^"]*/, it would mean:

<anything><anything-that-does-not-contain-{'}-{^}-or-{"}>

Again an error with that leading {*}. Assuming /.*^'^"*/:

<anything>{^}{'}{^}<any-number-(incl. zero)-of-{"}>

Yep. Requires an entire line of text that is without {'} or {"}.

To the OP, one note: a blank line will pass the above test.
Don't know if that matters to you or not. If it does, many
RE scanners will accept /^[^'"]+$/ which requires at least
ONE non-{'"} character.


--
|_ CJSonnack <Chris@Sonnack.com> _____________| How's my programming? |
|_ http://www.Sonnack.com/ ___________________| Call: 1-800-DEV-NULL |
|_____________________________________________|___ ____________________|


Similar Posts