Reputation: 13412
I got stuck with regexp to validate only numbers from 1-10 that could have two dashes(hyphens) before, for example:
--9
or
--10
or
--1
but not
--11 or not --0
I tried like seems to me everything, example:
/(-\-\[1-10])/
What is wrong?
Thanks a lot for so many working examples!!
What if I also wanted to validate to numbers before all of this, example:
8--10 but not 0--10 or not 11--11
I tried this but it didn't work:
/--([1-9]|10:[1-9]|10)\b/
Oh, this one works, finally:
/^(10|[1-9])--(10|[1-9])$/
Upvotes: 1
Views: 295
Reputation: 91518
Have a try with:
/\b(?:[1-9]|10)--(?:[1-9]|10)\b/
Change according to OP's edit.
Explanation:
The regular expression:
(?-imsx:\b(?:[1-9]|10)--(?:[1-9]|10)\b)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
[1-9] any character of: '1' to '9'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
10 '10'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
-- '--'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
[1-9] any character of: '1' to '9'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
10 '10'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
Upvotes: 3
Reputation: 51721
The correct regex is
/\b--([1-9]|10)\b/
You're incorrectly escaping the first [
of your character class as \[
. The character class used is incorrect as well. It would be treated as a character class with members 1
to 1
and a 0
i.e. [10]
which means it matches either 0
or 1
.
Also, the hyphens -
don't need to be escaped outside a character class []
. To validate the numbers that come before the hyphens as well use
/\b([1-9]|10)--([1-9]|10)\b/
Upvotes: 2
Reputation: 6390
Outside a character class, you don't need to escape hyphens. Also, your character class [1-10]
will only match 1
and 0
, because [1-10]
is equal to [10]
and that will only match 1
and 0
. Try this regex:
/^--(10|[1-9])$/
Upvotes: 2
Reputation: 456
When you write [1-10]
, it mean characters 1 to 1 + the 0 character. It as if you had write [0-1].
In fact, in your case, it would be better to test cases --1 to --9 and case --10 separately with something like : /^(--10)|(--[1-9])$/
You can test your regex on http://myregexp.com/
Upvotes: 1
Reputation: 37365
I guess this will fit
/\-\-([1-9]|10)\b/
if you don't want to capture your number, add ?:
:
/\-\-(?:[1-9]|10)\b/
Upvotes: 2