Regular expression condition - asp.net

My textbox should allow "The first character should be alphabetic or numbers and the remaining characters only numbers"
ex: #999999999 here # represent alphabet(a-z,A-Z) or numbers(0-9),
Please, help me.

I thought you need something like this:
in case you need 0 or more numbers after first symbol
[a-zA-Z0-9][0-9]*
in case you need 1 or more numbers after first symbol
[a-zA-Z0-9][0-9]+

Related

Do someone know where can I find all the symbols that denote letter, number, the end of the string, the beginning of the string in R?

I need to delimit the string and this time the delimiter is $($, but I need to note that the next character is number ( because I am specifically trying to separate the title from the year from one column. ) Even better would be that I could indicate, that after $($ there are 4 digits. But in general my question is where can I find all the symbols that denote different form of characters or group of character in order to make it easier to separate text into two columns. Thanks in advance.

Unable to understand ASP.NET regular expression example

I am going through the documnetation of ASP.NET on Regular Expressions where I stuck at following expression
^[0-9]{5}$
The Input (Mathches) are, 11111, 12345, 55555
Now, from my understanding , first and third are correct (First character can be 0-9 and there must be five occurrences of that character). Please make me understand how second is a valid match.
The {5} means that the match must be repeated, and [0-9] matches any digit. So this matches any 5 digits, not especially 5 identical digits.

Extract strings with exactly only two dots

I am looking to extract strings that have exactly 2 dots like below.
a.b.c
$$abc.$$def.123
The relevance is only to the dots.
So far i have tried
grep "\\.{2}" file_name.txt.
But this is not giving me the result. Could you please help me
I think this is just a regular expression issue. Your \.{2} will match two consecutive dots. What you'll probably want is something like:
^[^\.]*\.[^\.]*\.[^\.]*$
Which is "start of string, zero or more not-dots, a dot, zero or more not-dots, a dot, zero or more not-dots, end of string".

Regular expression for x number of digits and only one hyphen?

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$

regular expression for 5 digit number excluding '00000'

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>

Resources