Regex with xsd

Status
Not open for further replies.
Hello everyone. I have started working with regex and i was wondering if there is a way to simplify this expression. It is currently working. I just think there would be a way to simplify it.
I have been using this site for help on how to make it faster. http://regex101.com/

Here is my regex
Code:
^(([a-zA-Z]{2,}|[a-zA-Z]{2,}( [a-zA-Z]{2,}| [0-9]{1,})))( \/ [a-zA-Z]{2,}|[a-zA-Z]{2,}( [a-zA-Z]{2,}| [0-9]{1,}))*$

Here is the list of test string that should and should not match.
Code:
Matches:
example
example asd / example
example / example
example / example / example / example
example / example 343345 / example 2 / example
triggered
example 3
example 3 / example 3
example / example 3

Non-matches:
example / example /
example /  /
1 / a
a
123
1

Thanks for anyone that helps.
 
What is the purpose?

From the samples above, it looks to me like what you want is actually a string split, in which case you should use a string split and not a regular expression.

As i said above this is for an xsd file. ( XML Schema)
It is used to make sure the nodes in the Xml are formatted correctly.
Can't use string split in xsd.
 
In that case use a XML parser (validator?). You can't parse markup languages with regular expressions, they are on a separate level (heck if I remember the correct term, something with ring).

If you choose to keep using the regex above, then you should support multiple spaces with \s+, the range 0-9 can be written as \d, and {1,} can be replaced with a plus sign.
Finally, look into the look-behind operator ((?<EXPRESSION)), it fits your purpose.
 
Status
Not open for further replies.
Back
Top