Question

I need to write a restriction for a string column, so that it will include 20 letters and 3 of the following : (. , -).

How can this be done?

Thank You.

Was it helpful?

Solution

Please try this:

<xs:element name="someString">
  <xs:simpleType>
    <xs:restriction base="xsd:string">
      <xs:pattern value="([\w]*[\.\,\-]?[\w]*[\.\,\-]?[\w]*[\.\,\-]?[\w]*)"/>
        <xs:length value="23"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Explanation:

[\w\.\,\-]{0,20} match a single character present in the list below

  • Quantifier:

    Between 0 and 20 times, as many times as possible, giving back as needed [greedy]

    • \w match any word character [a-zA-Z0-9_]
    • \. matches the character .literally
    • \, matches the character , literally
    • \- matches the character - literally

So basically this will match all these

  1. aabbbcaabbbcaabbbcac,,-
  2. ,,-aabbbcaabbbcaabbbcac
  3. dd,aabbbc-aabbbc,aabbbc

The length value="23" forces that the total string be 23 characters and the regex forces that three characters of the total length be between comma, hyphen and period.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top