This expression is to check number > 1
^[1-9]+[0-9]*$
what is the expression to check if
it's greater than a given value, say
"99" ?
what about a value less than, 99?
Update:
I'm using ASP.NET validation control.
Thanks.
You say this is homework, so I'll give my answer for greater than 57 instead; you can take the idea and modify it.
/^([6-9][0-9]|5[89]|[1-9][0-9]{2,})$/
If you don't have the {a,} construct,
/^([6-9][0-9]|5[89]|[1-9][0-9][0-9]+)$/
I agree with #Rowlf comment! You should not need a regex for doing this (unless this is an interview question :) ) . Just use '>'.
Well, your given regex ^[1-9]+[0-9]*$ is matches >=1 not only >1
Related
I want to verify the Content of a box.
This box can have 2 good values out of 5 possible values and I want to look, whether the value in the box is one of the 2 good values.
How can I do it? How do I use the operator "OR"?
Please help me.
Thank you.
I think you will need to buffer the box content and then use an Expression Evaluation with ||. I would imagine the expression evaluation would look something like {B[Box Content]} == 'Value1' || {B[Box Content} == 'Value2'
You can use Regex!
For Example the Value is 1 and you want to verify that the Value is 1,2,3,4 OR 5 then use the following syntax:
{REGEX[1|2|3|4|5]}
I've been working on a project with lots of previous developer validation issues and am trying to clean up some issues and add validation in spots.
I generally use this simple regex that allows only positive whole values and can accept commas if they are entered:
"^([0-9]*,?)*$"
But in this case my program won't run if 0 is the value entered, so I need 1 to be the minimum value allowed. All else is the same. No decimals or any of that stuff.
I'm assuming I just need to add "not 0" to my regex, but not sure how to do that.
Allowed:
3,200
650
5
134560
100,000
Not Allowed:
0
3.2
-3.2
Thanks for your help!
Simply use this:
^[1-9]([0-9]*,?)*$
The first character cannot be a 0 and since it isn't optional either...
I made the following regex:
(\d{5}|\d-\d{4}|\d{2}-\d{3}|\d{3}-\d{2}|\d{4}-\d)
And it seems to work. That is, it will match a 5 digit number or a 5 digit number with only 1 hyphen in it, but the hyphen can not be the lead or the end.
I would like a similar regex, but for a 25 digit number. If I use the same tactic as above, the regex will be very long.
Can anyone suggest a simpler regex?
Additional Notes:
I'm putting this regex into an XML file which is to be consumed by an ASP.NET application. I don't have access to the .net backend code. But I suspect they would do something liek this:
Match match = Regex.Match("Something goes here", "my regex", RegexOptions.None);
You need to use a lookahead:
^(?:\d{25}|(?=\d+-\d+$)[\d\-]{26})$
Explanation:
Either it's \d{25} from start to end, 25 digits.
Or: it is 26 characters of [\d\-] (digits or hyphen) AND it matched \d+-\d+ - meaning it has exactly one hyphen in the middle.
Working example with test cases
You could use this regex:
^[0-9](?:(?=[0-9]*-[0-9]*$)[0-9-]{24}|[0-9]{23})[0-9]$
The lookahead makes sure there's only 1 dash and the character class makes sure there are 23 numbers between the first and the last. Might be made shorter though I think.
EDIT: The a 'bit' shorter xP
^(?:[0-9]{25}|(?=[^-]+-[^-]+$)[0-9-]{26})$
A bit similar to Kobi's though, I admit.
If you aren't fussy about the length at all (i.e. you only want a string of digits with an optional hyphen) you could use:
([\d]+-[\d]+){1}|\d
(You may want to add line/word boundaries to this, depending on your circumstances)
If you need to have a specific length of match, this pattern doesn't really work. Kobi's answer is probably a better fit for you.
I think the fastest way is to do a simple match then add up the length of the capture buffers, why attempt math in a regex, makes no sence.
^(\d+)-?(\d+)$
This will match 25 digits and exactly one hyphen in the middle:
^(?=(-*\d){25})\d.{24}\d$
i need to have a regular expression which accept atleast 1 digit number and maximum 5 digit number and if user enter zero in the following fashion like '00','000','0000','00000' then expression should reject such inputs.
currently, i am using ^[0-9]{1,5}$.
If you'd make sure that the user's input is formatted as a 5 digit number with leading zeroes, then the following regex would work:
^[0-9]{5}(?<!00000)$
This uses negative lookbehind to ensure that the string entered was not 5 zeroes.
^(?=.*[1-9].*)[0-9]{1,5}$
Uses a lookahead assertion to make sure there's at least one nonzero digit. If there is one, then the rest of the expression only matches if there's between 1 and 5 digits. Both conditions have to be met, or the expression won't match.
Agreed, though, that if you're trying to match a number, as opposed to a string of digits (like a ZIP code), it'd probably be better to compare numerically rather than with a regex.
Not nice but working:
from 1 to 99999: [1-9][0-9]{0,4}
from 01 to 09999: 0[1-9][0-9]{0,3}
from 001 to 00999: 00[1-9][0-9]{0,2}
from 0001 to 00099: 000[1-9][0-9]{0,1}
from 00001 to 00009: 0000[1-9]
And then putting all this together:
^(0[1-9][0-9]{0,3}|00[1-9][0-9]{0,2}|000[1-9][0-9]{0,1}|0000[1-9]|[1-9][0-9]{0,4})$
Edit: updated.
Does it really need to be a regular expression? Why not just check if 0 < value && value <=99999?
Ok I understand, here the solution :
^[1-9][0-9]{0,4}$
Take all number between 1 and 99999
Why don't you use a RangeValidator it is simplier.
<asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="Value must be greater than zero." ControlToValidate="tbQty" MinimumValue="1" MaximumValue="99999" Type="Integer"></asp:RangeValidator>
I want to use an ASP.NET RegularExpressionValidator to limit the number of words in a text box. (The RegularExpressionValidator is my favoured solution because it will do both client and server side checks).
So what would be the correct Regex to put in the RegularExpressionValidator that will count the words and enforce a word-limit? For, lets say, 150 words.
(NB: I see that this question is similar, but the answers given seem to also rely on code such as Split() so I don't think any of them could plug into a RegularExpressionValidator which is why I'm asking again)
Since ^ and $ is implicitly set with RegularExpressionValidators, use the following:
(\S*\s*){0,10}
The 0 here allows empty strings (more specifically 0 words) and 150 is the max number of words to accept. Adjust these as necessary to increase/decrease the number of words accepted.
The above regex is non-greedy, so you'll get a quicker match verses the one given in the question you reference. (\b.*\b){0,10} is greedy, so as you increased the number of words you'll see a decrease in performance.
Here is a quick reference for regular expressions:
http://msdn.microsoft.com/en-us/library/az24scfc.aspx
You can use this site to test the expressions:
http://regexpal.com/
Here is my regex example that works with both minimum and maximum word count (and fixes bug with leading spacing):
^\s*(\S+\s+|\S+$){10,150}$
Check this site:
http://lawrence.ecorp.net/inet/samples/regexp-validate.php#count
It's JavaScript RegEx, but it's very similar to asp.net
It's something like this:
(\b[a-z0-9]+\b.*){4,}