如何限制 XSD 中的字符串,使它们没有前导或尾随空格?
How to restrict strings in XSD so they don't have leading or trailing whitespace?
我正在尝试做一些非常简单的事情,但为什么这么难?我只想禁止前导和尾随空格,并保留字符串中的所有其他内容(包括中间空格)
<xs:simpleType name="NonEmptyString">
<xs:restriction base="xs:string">
<xs:minLength value="1" />
<xs:pattern value=".*[^\s].*"/> <!-- read my comments on sri's answer -->
</xs:restriction>
</xs:simpleType>
我真的超级难过
none 这些正则表达式可以在我的 XSD 甚至在线正则表达式测试器中为我工作(除了一些可以在在线测试器的单独框中使用 /g 但我无法弄清楚如何让它在 XSD)
中工作
我尝试了这些页面中的所有选项,none 会匹配:
- RegEx - removing preceding & trailing whitespace while matching contents - Stack Overflow
- XSD Element Not Null or Empty Constraint For Xml? - Stack Overflow
- Regex Leading and Trailing Whitespace - Regular Expression - Snipplr Social Snippet Repository
- How do I remove whitespace at the beginning or end of my string?
如果 "leading and trailing spaces" 你的意思是字符串的第一个字符和最后一个字符(如果有的话)都不能是空白 (U+0020),那么
[^ ](.*[^ ])?
应该没问题。如果也应该允许空字符串,使整个表达式可选:
([^ ](.*[^ ])?)?
第一个字符 class [^ ]
匹配前导非空白。 .*
允许在该初始字符之后的任何字符序列,以另一个非空白结束。由于没有前导或尾随空格的字符串集包括 "a" 和其他单字符字符串。 .*
和第二个字符 class 表达式被括在括号中并成为可选的。
如果 "leading and trailing spaces" 你的意思是“...空白”,那么最简单的方法就是使用 \S', the complement of the whitespace escape
\s`:
\S(.*\S)?
使用在线正则表达式测试器时,请记住正则表达式有许多不同的符号,并且 XSD 的模式方面总是尝试匹配整个输入文字,因此 XSD正则表达式对许多其他符号中使用的 ^
和 $
锚点没有用处。
我正在尝试做一些非常简单的事情,但为什么这么难?我只想禁止前导和尾随空格,并保留字符串中的所有其他内容(包括中间空格)
<xs:simpleType name="NonEmptyString">
<xs:restriction base="xs:string">
<xs:minLength value="1" />
<xs:pattern value=".*[^\s].*"/> <!-- read my comments on sri's answer -->
</xs:restriction>
</xs:simpleType>
我真的超级难过
none 这些正则表达式可以在我的 XSD 甚至在线正则表达式测试器中为我工作(除了一些可以在在线测试器的单独框中使用 /g 但我无法弄清楚如何让它在 XSD)
中工作我尝试了这些页面中的所有选项,none 会匹配:
- RegEx - removing preceding & trailing whitespace while matching contents - Stack Overflow
- XSD Element Not Null or Empty Constraint For Xml? - Stack Overflow
- Regex Leading and Trailing Whitespace - Regular Expression - Snipplr Social Snippet Repository
- How do I remove whitespace at the beginning or end of my string?
如果 "leading and trailing spaces" 你的意思是字符串的第一个字符和最后一个字符(如果有的话)都不能是空白 (U+0020),那么
[^ ](.*[^ ])?
应该没问题。如果也应该允许空字符串,使整个表达式可选:
([^ ](.*[^ ])?)?
第一个字符 class [^ ]
匹配前导非空白。 .*
允许在该初始字符之后的任何字符序列,以另一个非空白结束。由于没有前导或尾随空格的字符串集包括 "a" 和其他单字符字符串。 .*
和第二个字符 class 表达式被括在括号中并成为可选的。
如果 "leading and trailing spaces" 你的意思是“...空白”,那么最简单的方法就是使用 \S', the complement of the whitespace escape
\s`:
\S(.*\S)?
使用在线正则表达式测试器时,请记住正则表达式有许多不同的符号,并且 XSD 的模式方面总是尝试匹配整个输入文字,因此 XSD正则表达式对许多其他符号中使用的 ^
和 $
锚点没有用处。