HI I have a small validation to check if values are below 170000,
Here is what i have tried but it has some small issues. values can range from 0 - 170000
/^(?:[1-9]\d{0,4}|[1-6]\d{3}|170000)$/
Please some one rectify and explain this.
Why do you need regex for that?
Can't if (value <= 170000 && value >= 0) achieve your job?
/^\D*(\d{1,5}|1[0-6]\d{4}|170000)\D*$/
This will check the entire string. Otherwise, you need to fine-tune the regex boundaries.
\b(\d{1,5})\b|\b([1][0123456]\d{4})\b|\b(170000)\b
Demo :
http://regexr.com?328t5
Explained:
\b(\d{1,5})\b : Match all numbers from 0 to 99999
\b([1][0123456]\d{3})\b : Match 1 followed by 0 to 6, followed by any 4 digits i.e. 100000 to 169999
\b(170000)\b : Match 170000
Though using regexes for range validation is best avoided if possible, I think your problem lies with the second of your regex segments:
[1-9]\d{0,4}
[1-6]\d{3}
170000
The first will handle all numbers with lengths 1 thru 5 inclusive and the second should therefore handle six-digit numbers from 100000 up to (but not including) 170000 (which is handled by the third segment).
However, it's only handling four-digit numbers (which are already handled by the first segment anyway) so I suspect it should actually be 1[0-6]\d{4}.
In other words, /^(?:[1-9]\d{0,4}|1[0-6]\d{4}|170000)$/.
This solved it, was pretty easy and solved without RegExp :)
Others who posted RegExp for validation thanks to them too.
Ext.apply(Ext.form.field.VTypes, {
// vtype validation function
msrp: function(val, field) {
if(val >= 0 && val <= 170000){
return true;
}else{
return bundle.getMsg('vl.locate.label.ntavalidmsrp');
}
return validMaxMSRPTest.test(val);
},
// vtype Text property: The error text to display when the validation function returns false
msrpText: bundle.getMsg('vl.locate.label.ntavalidmsrp')
});
Related
i have had a go at matching a regular expression that is given in hh:mm:ss:nnn
with the range being :
00:00:00:000 for lowest
23:59:59:999 as the highest
a valid example is 06:07:22:575
invalid example being 6:7:22:475
so far i have given it a go and hasnt worked with this :
^(([0-1][1-9])|(2[1-3])):((0[1-9])|(1-5[0-9]:((0[1-9])|(1-5[0-9]:([1-9][1-9][1-9])$
but it hasnt worked.. can someone help me out
Try this regex:
^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]:[0-9][0-9][0-9]$
The only possibly tricky part here which might require explanation is the portion of the regex which covers the first two digits. In this case, we only want to support numbers from 0 to 23 inclusive. So, we use this pattern:
(?:[01][0-9]|2[0-3])
This says to match (and not capture) either a 0 or 1 followed by 0 to 9, i.e. 00-19, or a 2 followed by 0 to 3, i.e. 20-23.
Here is a working demo:
Demo
I think you just missed some square and round brackets?
^(([0-1][1-9])|(2[1-3])):((0[1-9])|([1-5][0-9])):((0[1-9])|([1-5][0-9])):([1-9][1-9][1-9])$
When you are trying to use check number between 00 and 59, it should be
( (0[1-9])|([1-5][0-9]) )
The one you tried (0[1-9])|(1-5[0-9] will only fetch 01~09
I would recommend:
/^[0-1][0-9]:[0-6][0-9]:[0-6][0-9]:[0-9][0-9][0-9]|^2[1-3]:[0-6][0-9]:[0-6][0-9]:[0-9][0-9][0-9]$/
Comprising two parts:
^[0-1][0-9]:[0-6][0-9]:[0-6][0-9]:[0-9][0-9][0-9]
^2[1-3]:[0-6][0-9]:[0-6][0-9]:[0-9][0-9][0-9]
This will match any time under 24 hours in the format listed above.
This can be seen working on Regex101 here.
const regex = /^[0-1][0-9]:[0-6][0-9]:[0-6][0-9]:[0-9][0-9][0-9]|^2[1-3]:[0-6][0-9]:[0-6][0-9]:[0-9][0-9][0-9]$/gm;
const strings = [
"00:00:00:000",
"23:59:59:999",
"06:07:22:575",
"6:7:22:475"
]
for (let i = 0; i < strings.length; i++) {
console.log(regex.test(strings[i]));
}
I am trying to write an expression which will allow :
1) Enter digits in the first five spots.
and
2) Enter of numbers and/or alphabets in the next 6 spots.
and
3) A check which validates that the total length is = 11 neither more nor less.
I am able to achieve the #1 & #2 through the below expression but not able to put a validation which checks for the total data to be exactly 11 neither less nor more. Can someone provide some inputs. Thanks
^([\d{5}]*[0-9a-zA-Z]{6})$
This part is failing: [\d{5}]*.
It matches "any digit, a {, a 5 or a }" 0 or more times.
You must be looking for \d{5}, which means "five digits"
This is what you want:
^\d{5}[0-9a-zA-Z]{6}$
for single line you could specify that your match must happen at the end of the line as follows ...
^\s*([0-9]{11})\z
hope it helps
this [\d{5}]* is causing the problem. Use this-
/^[0-9]{5}[a-zA-Z0-9]{6}$/
I have the following regex: [\-+]?[0-9]*[02468]$
which matches positive or negative even numbers, but I also want it to not match '0'. How can I accomplish this? I can't find a way to translate "Only match 0 as the final number if there are numbers that precede it" into regex language.
Thanks.
Maybe this one?
[\-+]?([0-9]*[1-9][0-9]*[02468]|[2468])$
EDIT
This allows leading zeroes on both alternations.
[-+]?0*([1-9][0-9]*[02468]|[2468])$
I came up with the following, which seems to work: [-+]?([1-9][0-9]*[24680]|[2468])$
It appears to avoid leading zeros and doesn't use lookback. Correct me if I am wrong.
This would eliminate numbers that start with 0:
[\-+]?[^0][0-9]*[02468]$
So it would exclude 0.
The drawback is it wouldn't work for 012.
So it could be a solution as long as your numbers don't have leading zeros.
I don't understand the need for a regex here, though (you never specify the context).
It would be better (if possible) to check if the number is non-zero and even:
( num != 0 && num % 2 == 0 )
I am trying to create a regular expression that determines if a string (of any length) matches a regex pattern such that the number of 0s in the string is even, and the number of 1s in the string is even. Can anyone help me determine a regex statement that I could try and use to check the string for this pattern?
So completely reformulated my answer to reflect all the changes:
This regex would match all strings with only zeros and ones and only equal amounts of those
^(?=1*(?:01*01*)*$)(?=0*(?:10*10*)*$).*$
See it here on Regexr
I am working here with positive lookahead assertions. The big advantage here of a lookahead assertion is, that it checks the complete string, but without matching it, so both lookaheads start to check the string from the start, but for different assertions.
(?=1*(?:01*01*)*$) does check for an equal amount of 0 (including 0)
(?=0*(?:10*10*)*$) does check for an equal amount of 1 (including 0)
.* does then actually match the string
Those lookaheads checks:
(?=
1* # match 0 or more 1
(?: # open a non capturing group
0 # match one 0
1* # match 0 or more 1
0 # match one 0
1* # match 0 or more 1
)
* # repeat this pattern at least once
$ # till the end of the string
)
So, I have come up with a solution to the problem:
(11+00+(10+01)(11+00)\*(10+01))\*
For even sets of 0s, you can use the following regex to ensure that the number of 0s is even.
^(1*01*01*)*$
However, I believe that the question is to have both an even number of 0s and also an even number of 1s. Since it is possible to construct a non-deterministic finite automaton (NFA) for this problem, the solution is regular and can be represented using a regex expression. The NFA is represented via the machine below, S1 is the start/exit state.
S1 ---1----->S2
|^ <--1----- |^
|| ||
00 00
|| ||
v| v|
S3----1----->S4
<---1------
From there, there's a way to convert NFAs to regex expressions but it's been a while since my computation course. There's some notes below that seem to be helpful in explaining the steps required to convert a NFA to a regex.
http://www.cs.uiuc.edu/class/sp09/cs373/lectures/lect_08.pdf
RE-UPDATED
Try this : [ check out this demo : http://regexr.com?30m7c ]
^(00|11|0011|0110|1100|1001)+$
Hint :
Even numbers are divisible by 2, thus - in binary - they always end in zero (0)
Not a regular expression (which is likely to be impossible, although I can't prove it: the proof by contradiction via the pumping lemma fails), but the "correct" solution is avoiding a complicated and inefficient regular expression all together and using something like (in Python):
def even01(string):
return string.count("1") % 2 == 0 and string.count("0") % 2 == 0
Or if the string has to consist only of 1s and 0s:
import re
def even01(string):
return not re.search("[^01]",string) and \
string.count("1") % 2 == 0 and string.count("0") % 2 == 0
^(0((1(00)*1)*0|1(11|00)*01)|1((0(11)*0)*1|0(11|00)*10))*$
If I haven't overlooked anything, this matches any bit string where the number of 0s is even and the number of 1s is even, using only rudimentary regex operators (*, ^, $). It's slightly easier to see how it works if written like this:
^(0((1(00)*1)*0
|1(11|00)*01)
|1((0(11)*0)*1
|0(11|00)*10))*$
The following test code should illustrate the correctness - we compare the result of the pattern match against a function that tells us if a string has an even number of 0s and 1s. All bit strings of length 16 are tested.
import re
balanced = lambda s: s.count('0') % 2 == 0 and s.count('1') % 2 == 0
pat = re.compile('^(0((1(00)*1)*0|1(11|00)*01)|1((0(11)*0)*1|0(11|00)*10))*$')
size = 16
num = 2**size
for i in xrange(num):
binstr = bin(i)[2:].zfill(size)
b, m = balanced(binstr), bool(pat.match(binstr))
if b != m:
print "balanced('%s') = %d, pat.match('%s') = %d" % (binstr, b, binstr, m)
break
elif i != 0 and i % (num / 10) == 0:
# Python 2's `/` operator performs integer division
print "%d percent done..." % (100 * i / num + 1)
If you try to solve within the same sentence (starting with ^ and ending with $), you are in deep trouble. :-)
You can make sure that you have an even number of 0s (with ^(1*01*01*)*$, as stated by #david-z) OR you can make sure that you have an even number of 1s:
^(1*01*01*)*$|^(0*10*10*)*$
It works for strings with small lengths as well, such as "00" or "101", both valid strings.
I have also been working on lookaheads and lookbacks in my spare time, and using lookahead the problem can be solved while taking also account for the single 1s and/or the single 0s. So, the expression should also work for 11,1111,111111,... and also for 00,0000,000000,....
^(((?=(?:1*01*01*)*$)(?=(?:0*10*10*)*$).*)|([1]{2})*|([0]{2})*)$
Works for all cases.
So, if the string consists of only 1s or only 0s:
([1]{2})*|([0]{2})*
If it contains a mix of 0s and 1s, the positive lookahead will take care of that.
((?=(?:1*01*01*)*$)(?=(?:0*10*10*)*$).*
Combining both of them, it takes into account all string with even number of 0s and 1s.
I have a need to search all numbers with 4 digits between 2000 and 3000.
It can be that letters are before and after.
I thought I can use [2000-3000]{4}, but doesnt work, why?
thank you.
How about
^2\d{3}|3000$
Or as Amarghosh & Bart K. & jleedev pointed out, to match multiple instances
\b(?:2[0-9]{3}|3000)\b
If you need to match a3000 or 3000a but not 13000, you would need lookahead and lookbefore like
(?<![0-9])(?:2[0-9]{3}|3000)(?![0-9])
Regular expressions are rarely suitable for checking ranges since for ranges like 27 through 9076 inclusive, they become incredibly ugly. It can be done but you're really better off just doing a regex to check for numerics, something like:
^[0-9]+$
which should work on just about every regex engine, and then check the range manually.
In toto:
def isBetween2kAnd3k(s):
if not s.match ("^[0-9]+$"):
return false
i = s.toInt()
if i < 2000 or i > 3000:
return false
return true
What your particular regex [2000-3000]{4} is checking for is exactly four occurrences of any of the following character: 2,0,0,0-3,0,0,0 - in other words, exactly four digits drawn from 0-3.
With letters before an after, you will need to modify the regex and check the correct substring, something like:
def isBetween2kAnd3kWithLetters(s):
if not s.match ("^[A-Za-z]*[0-9]{4}[A-Za-z]*$"):
return false
idx = s.locate ("[0-9]")
i = s.substring(idx,4).toInt()
if i < 2000 or i > 3000:
return false
return true
As an aside, a regex for checking the range 27 through 9076 inclusive would be something like this hideous monstrosity:
^2[7-9]|[3-9][9-9]|[1-9][0-9]{2}|[1-8][0-9]{3}|90[0-6][0-9]|907[0-6]$
I think that's substantially less readable than using ^[1-9][0-9]+$ then checking if it's between 27 and 9076 with an if statement?
Hum tricky one. The dash - only applies to the character immediately before and after so what your regex is actually matching is exactly 4 characters between 0 and 3 inclusive (ie, 0, 1, 2 and 3). eg, 3210, 1230, 3333, etc... Try the expression below.
(2[0-9]{3})|(3000)
Here's explanation why and ways to detect ranges: http://www.regular-expressions.info/numericranges.html
Correct regex will be \b(2\d{3}|3000)\b. That means: match character '2' then exactly three digits (this will match any from 2000 to 2999) or just match '3000'. There are some good tutorials on regular expressions:
http://gnosis.cx/publish/programming/regular_expressions.html
http://immike.net/blog/2007/04/06/the-absolute-bare-minimum-every-programmer-should-know-about-regular-expressions/
http://www.regular-expressions.info/
why don't you check for greater or less than? its simpler than a regex
num >= 2000 and num <=3000