I was wondering how to make a regular expression for any character except *
and +
. I\'ve tried ([^*+])
and (\\[^*+])
but
You were close:
[^*+]
Notice that there's no need for escaping those characters inside square brackets, because they have no special meaning there.
EDIT: according to http://www.regular-expressions.info/charclass.html:
Note that the only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (), the caret (^) and the hyphen (-). The usual metacharacters are normal characters inside a character class, and do not need to be escaped by a backslash. To search for a star or plus, use [+*]. Your regex will work fine if you escape the regular metacharacters inside a character class, but doing so significantly reduces readability.
So the problem is not in the character escaping. Maybe you need to match all those occurences. In that case, look up @Cfreak's answer.
In the XML Schema regex flavor, you must not add regex delimiters (i.e., the /
at either end of /^[^*+]+$/
). You also don't need to use anchors (i.e., the ^
at the beginning and $
at the end); all regex matches are automatically anchored at both ends. That line should read:
<xsd:pattern value="[^*+]+"></xsd:pattern>
...meaning the whole element must consist of one or more of any characters except *
and +
.
You need to test the entire string. But you don't actually have to escape it in the character class:
/^[^*+]+$/