ABCD
ABCD

Reputation: 11

Regexp negate xml element name

this maybe dumb question but could someone please negate this regexp for me?

$reg = '/^(?!XML)[a-z][\w0-9-]*$/i';

I've tried [^...] but not really works or I'm just doing it wrong. Thanks in advance!

Upvotes: 1

Views: 94

Answers (2)

mpyw
mpyw

Reputation: 5754

Try this:

$positive = '\A(?!XML)[a-z][\w-]*+\z';
$negative = "(?!$positive)\A";

Upvotes: 0

Amal Murali
Amal Murali

Reputation: 76646

[^...] syntax works only for character classes. Outside a character class, ^ asserts position at the beginning of the line. It cannot be used to negate a regular expression itself. What you can do it instead, is to do the negation using the PHP code: if(!preg_match(...))

$reg = '/^(?!XML)[a-z][\w0-9-]*$/i';

if(!preg_match($reg, $stuff)) {
    # code...
}

However, if you're going to work with XML / HTML, then a regex isn't probably the best way. Use an XML parser instead. That way, you can be sure that your code doesn't break when the format of the mark up changes.

Upvotes: 1

Related Questions