INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

XSD Restriction pattern

XSD Restriction pattern

(OP)
Hi,
I'm trying to write a regex pattern for my schema. I want to prohibit the use of certain characters ("&'#) & --
I have the following pattern that works to a point:

[a-zA-Z\s\d_\. !$%^*()_+={}|:;,?~/£@\\]*

I'm having trouble restricting the hyphen '-' to a single char as I need to exclude double hyphen '--'

I've tried a few things, such as

[a-zA-Z\s\d_\. !$%^*()_+={}|:;,?~/£@\\]*[-]?

but it's now driving me crazy. Any help would be most welcome.

RE: XSD Restriction pattern

I think probably your solution will require grouping. I am going to use a simpler pattern that I have used to restrict to a hyphenated data name, that excludes adjacent hyphens, as my example (I found this a while back via Google search). You can expand the set of characters [A-Za-z0-9] as needed for your situation.

CODE --> RegEx

^[A-Za-z0-9]+(-[A-Za-z0-9]+)* 

The parentheses define a group. The group is defined as a hyphen followed by one or more non-hyphen characters. The group can appear zero or more times. The the beginning of the RegEx defines the same set of non-hyphen characters, that can be repeated one or more times. So, there is no way for a hyphen to appear adjacent to another hyphen, or at the beginning or end (which was a requirement for me).

I hope this helps...

Tom Morrison
Hill Country Software

RE: XSD Restriction pattern

\-([^\-]|$) will match a hyphen that is followed by a character that is not a hyphen or by the end of the string.

Based on your first pattern:

CODE --> RegExpr

([a-zA-Z\s\d_\. !$%^*()_+={}|:;,?~/£@\\]|\-([^\-]|$))* 

RE: XSD Restriction pattern

(OP)
Both, thanks for your input, this has taken up the majority of my day.
K5tm, I tried grouping. several variations. I think it was the greedy " that was the route of my problem but I just couldn't find a way around it.
atlopes, many thanks. I've left the office for the day but will give that a go tomorrow and let you know how I get on.

thanks again.
Chris

RE: XSD Restriction pattern

atlopes, doesn't the [^\-] allow characters from the unwanted set to follow a hyphen?

Tom Morrison
Hill Country Software

RE: XSD Restriction pattern

(OP)
atlopes, at first test that pattern works however, as k5tm points out it allows the unwanted characters to follow a hyphen.
but it's still a step forward from what I had yesterday.

RE: XSD Restriction pattern

Yes, Tom's remark was spot on.

A new alternative, better tried:

CODE --> RegExpr

^([a-zA-Z\s\d_\. !$%^*()_+={}|:;,?~/£@\\]|-([a-zA-Z\s\d_\. !$%^*()_+={}|:;,?~/£@\\]|$))+$ 

RE: XSD Restriction pattern

(OP)
Fantastic!

I've made a slight amendment to include characters I'd missed from the pattern in the OP and removed the anchors, but this seems just the ticket.

CODE

<xs:pattern value="([a-zA-Z\s\d_\. !$%^*\[\]()_+={}|:;,?~/£€@\\]|-([a-zA-Z\s\d_\. !$%^*\[\]()_+={}|:;,?~/£€@\\]|$))+"/> 

RE: XSD Restriction pattern

(OP)
One further amendment. I found the pattern I posted last was restricting a hyphen by the end of the string.

so I now have :

CODE

<xs:pattern value="([a-zA-Z\s\d_\. !$%^*\[\]()_+={}|:;,?~/£€@\\]|-([a-zA-Z\s\d_\. !$%^*\[\]()_+={}|:;,?~/£€@\\]|$))+[-$]?"/> 

RE: XSD Restriction pattern

Without the anchors, the |$ inside the group following the hyphen becomes redundant, so you can also get rid of it.

Tried finally with an XSD editor (not just with a Regular Expression editor): it's your corrections that make this work.

RE: XSD Restriction pattern

A final question: do you pretend to allow a single hyphen as the value for the element / attribute?

RE: XSD Restriction pattern

(OP)
Sorry I don't understand the question. A single hyphen is allowed, what do you mean 'pretend'?

RE: XSD Restriction pattern

Given your pattern

CODE --> XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="root">
    <xs:complexType>
      <xs:attribute name="testPattern">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:pattern value="([a-zA-Z\s\d_\. !$%^*\[\]()_+={}|:;,?~/£€@\\]|-([a-zA-Z\s\d_\. !$%^*\[\]()_+={}|:;,?~/£€@\\]|$))+[-$]?"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
    </xs:complexType>
  </xs:element>
</xs:schema> 
testPattern="-" is not allowed, as is testPattern="+", for instance (- must be followed by some other allowed character, or must be preceded by some other allowed character and be placed at the end of the string). This may be, or not, what you do "pretend" (sorry about the terminology, English is not my native language and I might be following a false friend, here).

RE: XSD Restriction pattern

Just about the English: Pretend means "to do as if", I think you meant "specify" rather than pretend, atlopes.

There is a synonym for both things in german, for example: "vorgeben" in one context can mean to specify and in another means to pretend, in the sense of specifying something untrue, the full verb then is "vorgeben zu sein (oder haben)", which actually makes it another verb via the auxiliary verbs "haben" or "sein". Maybe something similar is working in your native language, too, but I would guess you're rather spanish or mexican perhaps? I may be wrong there thinking of Lopez.

So the question likely is whether the specification should allow a single "-" only as match of the pattern. Maybe really very unimportant as it would be a rare corner case.

Bye, Olaf.

RE: XSD Restriction pattern

Olaf,

Quote (Olaf)

I may be wrong there

You left out Portuguese - which I think has a good chance of being correct.

Thanks for the interesting German lesson. I am several decades removed from my days when I actually could converse in German. I never learned this type of auxiliary verb.

Tom Morrison
Hill Country Software

RE: XSD Restriction pattern

Yes, I was really following a false friend: in Portuguese, "pretender" (a verb) means not only "to pretend", but also "to intend". I hope to not forget it in the future, because looking back I think I used it wrongly more than once.

As for the name, when you find a surname that looks like Spanish but ends in "-es" instead of "-ez", then most likely it's a Portuguese name (like "Gomes", instead of "Gomez", "Lopes" instead of "Lopez", and so on - but they share the same semantics, meaning "the son of", like the "-son" suffix in English, or "-ic" in Slavic languages, ...).

RE: XSD Restriction pattern

So it's about the same, OK.

Tom, auxiliary verbs are not a german only thing, the term alone exists in English, too: Be, have, will, do... So german has that kind of compound verbs, too, not always put together into one word.
And nice to know. So "z" vs "s" is about the same difference as in US English vs UK English ;)

Bye, Olaf.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close