0

I am using XSLT with regexp:match exslt function. The said function takes JavaScript Regex pattern. Therefore I am trying to match a set of numbers 1 thru 3 OR 5 thru 7 OR 9 thru 23.

Following is the regex pattern I've come up with:

(^[1-3]$|^[5-7]$|^[9-23]{1,2}$)

This regex does NOT match with any value at all. Following alternate pattern is good only to a little extent:

(^[1-3]$|^[5-7]$|^9$|^[10-23]{2}$)

While this matches with all other expected number values except 14 thru 19. Why is it so and how to make the Regex good. BTW, I am using http://www.regextester.com/ to test the pattern matching.

Thank you.

1
  • At first glance, the or operator seems fishy. It might do $|^ instead of tokenGroup|otherTokenGroup, which would make sense. Commented Nov 4, 2011 at 17:30

4 Answers 4

2

Brackets contain characters, not strings. Try this:

^([1-35-79]|1[0-9]|2[0-3])$

Edit: Tested it, it works.

var str="";
for (var i=0; i<30; i++) {
  if (/^([1-35-79]|1[0-9]|2[0-3])$/.test(i+"")) str+=i+' '
}
console.log(str);

prints

1 2 3 5 6 7 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your quick answer. I have used the regex pattern in XSLT however your solution was helpful for me to understand.
0

You are working with strings not numbers so 14 or 19 or 20 are strings of digits not the numbers.

so [10-23]{2} will match 0-2, 1,3 or 0,1,2,3 two times. Notice the missing 4-9.

(^[1-3]$|^[5-7]$|^9$|^1[0-9]$|^2[0-3]$)

1 Comment

Thank you that helped me understand why it was not giving the wanted result.
0

In regexes, stuff in brackets only denotes one character. Try:

^[1-3]$|^[5-7]$|^9$|^1[0-9]$|^2[0-3]$

Comments

0

Convert it to an integer, test for >0 and < 24 and !=4 and !=8. Keep it simple.

1 Comment

Yes this is simple and works too. I've oversimplified the problem statement to apply that into the problem context. A different view point of looking at the issue though. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.