Regular Expression, pattern matching in xsd

后端 未结 3 512
梦谈多话
梦谈多话 2020-12-16 15:58

I was wondering how to make a regular expression for any character except * and + . I\'ve tried ([^*+]) and (\\[^*+]) but

相关标签:
3条回答
  • 2020-12-16 16:34

    You were close:

    [^*+]
    

    enter image description here

    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.

    0 讨论(0)
  • 2020-12-16 16:41

    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 +.

    0 讨论(0)
  • 2020-12-16 16:41

    You need to test the entire string. But you don't actually have to escape it in the character class:

    /^[^*+]+$/
    
    0 讨论(0)
提交回复
热议问题