Regular Expression based search in CFEClipse for tags not containing an attribute
-
12-01-2021 - |
题
I've got a few regular expressions down pat, but am struggling within CFEclipse to nail the syntax which finds:
all instances of the cfquery tag that don't contain the attribute name
I've tried
<cfquery [^>]*(?!(name=))[^>]*>
which I'd intended to trap:
the cfquery tag, followed by any number of characters that aren't the closing >, NOT followed by the name attribute, followed by any number of characters that aren't the closing >, followed by the closing >.
This finds plenty of matches, the some of which do contain the name attribute and some of which don't (so it's obviously incorrect).
Can anyone hit me with a clue stick on this one? Thanks!
解决方案
It looks like you should be using an XML parser for this, but your issue is that [^>]*
is greedy and will match through the name attribute if it exists. You need something like the following:
<cfquery (?![^>]*name=)[^>]*>
By moving the [^>]*
into the negative lookahead you can make sure that "name=" does not exist in the string before the next >
.
其他提示
If you want your lookahead to act against each character until the end of the opening tag, you need to split the character class and the quantifier, like so:
<cfquery\b(?:(?!name=)[^>])*>
Note the \b
(word boundary) instead of space - this will allow <cfquery>
to match (whilst still blocking <cfqueryparam...>
tags. (\b
after an alphanumeric character ensures the next character is non-alphanumeric.)
Also worth pointing out, the (?:
...)
is a non-capturing group - similar to normal (
...)
but doesn't create a backreference since we don't need it.
Of course, there is a small possibility of this not matching correctly - if you have an attribute notname
or you have name = "whatever"
these will both be treated incorrectly.
To solve this, we can use a \b
again before the name this time, and then use an optional \s
to allow (but not require) any whitespace between the attribute name and the equals sign:
<cfquery\b(?:(?!\bname\s*+=)[^>])*>
And don't forget to make sure case-insensitive flag is enabled, if you might have tags/attributes in mixed/uppercase anywhere.