Reputation: 41
I want to match a 3-digit number only from a webpage.
So for example if webpage has number 1 599-+
(white space between 1
and 5
and -+
signs after). I only want to capture/match numbers between 0
and 599-+
and nothing else.
My regex is: regex(?:^|(?:[^\d\s]\s*))([0-5]\d\d-+)
but this one also matches "i 1599-+"
or regex(\^[0-5]?[0-9]?[0-9]-+$)
doesnt work either...
Upvotes: 2
Views: 859
Reputation: 1796
From the above I understood and developed this, I think this is what you needed.
/^[0-5]?[0-9]?[0-9][\+|-]?$/.test("599");
In the above regex I made + - as optional and it'll check for presence of any one sign.
If you want in the order of -+ then try this
/^[0-5]?[0-9]?[0-9][\-][\+]$/.test("99-+");
.
Okay @user3214294
Upvotes: 0
Reputation: 382150
A solution would be to use this regular expression with a non capturing group matching either the start of the string or something that's not a digit (with a little more verbosity due to space handling) :
(?:^|(?:[^\d\s]\s*))([0-5]\d\d)
Examples (in javascript as you didn't specify a language) :
"1 599".match(/(?:^|(?:[^\d\s]\s*))([0-5]\d\d)/) => null
"a sentence with 1 599 inside".match(/(?:^|(?:[^\d\s]\s*))([0-5]\d\d)/) => null
"another with 599".match(/(?:^|(?:[^\d\s]\s*))([0-5]\d\d)/) => ["h 599", "599"]
"599 at the start".match(/(?:^|(?:[^\d\s]\s*))([0-5]\d\d)/) => ["599", "599"]
(desired group is at index 1)
Upvotes: 3
Reputation: 1796
I hope this is needed for you.Try it, if it is not fulfilling.Write a little more description.
/^[0-5]?[0-9]?[0-9]$/.test("599");
Upvotes: 1