Regular Expression help

Associate
Joined
16 Jun 2009
Posts
756
Does anyone know what format the regular expression would be if i wanted to limit input to betting odds format, ie x/y where x and y are positive integers. I Just cant get my head round how you format these things.
 
Does anyone know what format the regular expression would be if i wanted to limit input to betting odds format, ie x/y where x and y are positive integers. I Just cant get my head round how you format these things.

If you want to get the hang of regular expressions quickly a book that really helped me out is called "Regular Expression Pocket Reference". Well worth the £5 or so it costs.
 
/^\d+\/\d+$/ won't work since it will match 0/1

The leading and trailing forward slashes are not part of the regular expression but language specific delimiters.
 
/^\d+\/\d+$/ won't work since it will match 0/1

The leading and trailing forward slashes are not part of the regular expression but language specific delimiters.

The slashes were added to demonstrate the need to escape the middle slash, should the language chosen require delimiters and said delimiters be slashes.

New pattern:
Code:
/^(?:[1-9]\d*)+\/(?:[1-9]\d*)+$/
 
Last edited:
/^(?:[1-9]\d*)+\/(?:[1-9]\d*)+$/ seems overly complex to me.

My example does, I believe, what was asked for - no more, no less.

Here it is again with explanatory notes.


^[1-9]\d*\/[1-9]\d*$

* [Assert position at the beginning of the string][1] `^`
* [Match a single character in the range between “1” and “9”][2] `[1-9]`
* [Match a single character that is a “digit” (any decimal number in any Unicode script)][3] `\d*`
* [Between zero and unlimited times, as many times as possible, giving back as needed (greedy)][4] `*`
* [Match the character “/” literally][5] `\/`
* [Match a single character in the range between “1” and “9”][2] `[1-9]`
* [Match a single character that is a “digit” (any decimal number in any Unicode script)][3] `\d*`
* [Between zero and unlimited times, as many times as possible, giving back as needed (greedy)][4] `*`
* [Assert position at the end of the string, or before the line break at the end of the string, if any (line feed)][1] `$`

Created with [RegexBuddy](http://www.regexbuddy.com/)

[1]: http://www.regular-expressions.info/anchors.html
[2]: http://www.regular-expressions.info/charclass.html
[3]: http://www.regular-expressions.info/shorthand.html
[4]: http://www.regular-expressions.info/repeat.html
[5]: http://www.regular-expressions.info/characters.html#special
 
I am simply offering the OP an explanation of how the regex is constructed in the hope that he will find it helpful.

If I am missing something in the solution you have offered then please explain as I am always keen to learn.

I am sorry if you find my questioning defensive, it is not meant that way.
 
Last edited:
Back
Top Bottom