如何限制 XSD 中的字符串,使其没有前导或尾随空格? [英] How to restrict strings in XSD so they don't have leading or trailing whitespace?

查看:27
本文介绍了如何限制 XSD 中的字符串,使其没有前导或尾随空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些非常简单的事情,但为什么这么难?我只想禁止前导和尾随空格,并使字符串中的其他所有内容保持不变(包括中间空格)

I'm trying to do something very simple, but why is it so hard? I just want to disallow leading and trailing whitespace and leave everything else in the string intact (including middle 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>

我真的超级难受

这些正则表达式在我的 XSD 甚至在线正则表达式测试器中都不适用于我(除了有些可以在在线测试器的单独框中使用/g 但我不知道如何获得它在 XSD 中工作)

none of these regexes will work for me either in my XSD or even in online regex testers (except some will work with the /g in a separate box on the online tester but I can't figure out how to get that to work in XSD)

我从这些页面尝试了所有这些,没有一个匹配:

I tried all of them from these pages, none would match:

推荐答案

如果用前导和尾随空格"表示字符串的第一个字符和最后一个字符(如果有)都不能是空格 (U+0020),然后

If by "leading and trailing spaces" you mean that neither the first character nor the last character (if any) of the string may be a blank (U+0020), then

[^ ](.*[^ ])?

应该没问题.如果还应允许空字符串,则将整个表达式设为可选:

should do fine. If the empty string should also be allowed, make the entire expression optional:

([^ ](.*[^ ])?)?

第一个字符类 [^ ] 匹配前导非空白..* 允许在该初始字符之后的任何字符序列,以另一个非空白结尾.由于没有前导或尾随空格的字符串集包括a"和其他单字符字符串..* 和第二个字符类表达式用括号括起来并成为可选的.

The first character class [^ ] matches a leading non-blank. The .* allows any sequence of characters after that initial character, ending with another non-blank. Since the set of strings with no leading or trailing spaces includes "a" and other single-character strings. the .* and the second character class expression are wrapped in parentheses and made optional.

如果前导和尾随空格"是指...空白",那么最简单的方法是使用 \S',空白转义的补充\s`:

If by "leading and trailing spaces" you mean "... whitespace", then the simplest thing would be to use \S', the complement of the whitespace escape\s`:

\S(.*\S)?

使用在线正则表达式测试器时,请记住正则表达式有许多不同的表示法,并且 XSD 的模式方面总是尝试匹配整个输入文字,因此 XSD 正则表达式对 ^$ 锚点在许多其他符号中使用.

When using online regex testers, do bear in mind that there are many different notations for regular expressions, and XSD's pattern facets always try to match the entire input literal, so XSD regular expressions have no use for the ^ and $ anchors used in many other notations.

这篇关于如何限制 XSD 中的字符串,使其没有前导或尾随空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆