I'm trying to write a simple regex but I don't know why it is not working.
User enter 2 digits number like 01, 09, 23, 55, until 82. After 82 system will refuse.
Here is my regex, 2 digits must be smaller than 82.
0[1-9]|[1-8][0-9]|8[0-2]
You should have [1-7] for the range 10-79, not [1-8]. Don't forget the ^ and $ to specify the start and ending of the string:
^(0[1-9]|[1-7]\d|8[0-2])$
Why not cast to an integer and then just test x < 82?
Your second part is wrong. It'll match from 10 to 89, whereas you want it to match from 10 to 79 and let the third part handle 80 to 82.
0[1-9]|[1-7][0-9]|8[0-2]
Related
I am trying to build a regex for following rules
No characters – only numbers are allowed to enter
Numbers must contain not less than 8 digits
Numbers and combinations, starting as follows should not be allowed:
[3.1] - Any number starting with 0 (zero) and with 1 (one);
[3.2] - Numbers starting with 20, 21, 22, 23, 24, 25, 26, and 27.
I am able to achieve regex for points 1, 2 and 3.1 like this
^[2-9]{1}[0-9]{7,}$
But I am not able to find solution for point 3.2
This is one of the options I started with, this regex matches the string that must start with 20, 21, 22, 23, 24, 25, 26, or 27.
^([2][0-7])[2-9]{1}[0-9]{7,}$
I just have to find the negation of this first condition.
Please help!
Rather than trying to negate the 20-27 condition, just match numbers that start with 28 or 29 instead:
^(?:2[89]|[3-9]\d)\d{6,}$
Demo on regex101
Looking for a regex in C++ that would add integers and fractions on their own to the regex iterator.
So say we have the current string given
12, 144, 18, 12/13, 92, a, 34, 8, 52
I'm looking to have 12 144 12/13 all added, without the comma or 12 added twice (or 13)
Currently have \d+?/\d+|\d+[^/]
however this also grabs anything after an integer (i.e. the comma)
What am I missing here to make sure I grab the int and not the comma whilst making sure I don't grab numbers from fractions twice?
I beseech thee, oh gods of the mighty RegEx... hear my plee!!!
I need a Regex for the following rules
-Number has to be 6 characters long
-Number has to start with 5
Number cannot start with "50****" or "589***"(It can start with 51, 52, 587, 583, etc...)
What I'm working off of right now is
^5(?!(0\b|89\b))\d+\b.
Please HELP!!!
^5(?!(?:0|89))\d{5}$
This should do it for you.See demo.
https://regex101.com/r/hE4jH0/1
You dont need \b after 0 or 89 as you dont expect a word boundary there.
In an application I have the need to validate a string entered by the user.
One number
OR
a range (two numbers separated by a '-')
OR
a list of comma separated numbers and/or ranges
AND
any number must be between 1 and 999999.
A space is allowed before and after a comma and or '-'.
I thought the following regular expression would do it.
(\d{1,6}\040?(,|-)?\040?){1,}
This matches the following (which is excellent). (\040 in the regular expression is the character for space).
00001
12
20,21,22
100-200
1,2-9,11-12
20, 21, 22
100 - 200
1, 2 - 9, 11 - 12
However, I also get a match on:
!!!12
What am I missing here?
You need to anchor your regex
^(\d{1,6}\040?(,|-)?\040?){1,}$
otherwise you will get a partial match on "!!!12", it matches only on the last digits.
See it here on Regexr
/\d*[-]?\d*/
i have tested this with perl:
> cat temp
00001
12
20,21,22
100-200
1,2-9,11-12
20, 21, 22
100-200
1, 2-9, 11-12
> perl -lne 'push #a,/\d*[-]?\d*/g;END{print "#a"}' temp
00001 12 20 21 22 100-200 1 2-9 11-12 20 21 22 100-200 1 2-9 11-12
As the result above shows putting all the regex matches in an array and finally printing the array elements.
I have something like-
[[59],
[73 41],
[52 40 09],
[26 53 06 34],
[10 51 87 86 81],
[61 95 66 57 25 68]]
I need to add a comma before every space to be like -
[[59],
[73, 41],
[52, 40, 09],
[26, 53, 06, 34],
[10, 51, 87, 86, 81],
[61, 95, 66, 57, 25, 68]]
What would be regex string for that?
Judging from your data, you may just replace a space ' ' by a comma followed by a space ', '. You do not need a regex for that.
This depends on what regex flavor you are using but in general, looking for matches would be
(\d+)\s
and replacing would be
\1,
In Notepad++, open up the find control window with Ctrl+H.
In Find What put a single space character
In Replace With put a comma followed by a space character
This gives the expected output, but isn't very interesting as far as Regexes go.
s/\( \)/,\1/g
And, as an afterthought:
s/ /, /g
Why bother with substitution replacement? :)
Replace (\d)\s(\d) with
\1, \2