Regular expression to match 0-2 characters followed by 4 digits - c#

I'd like to make a regular exp for C#. The 1st two letters are characters which are optional and then 4 digits which are mandatory.
as:
4584
0259
0015
G3227
G3277
G4018
G3737
G3737
G3277
GU4444
GU4444
G3277
G3277
G3988
C3737
G3227
G3227

I suggest this:
\b\p{L}{0,2}\d{4}\b
This would be for finding text like this in a larger string. If you want to validate a string instead, use
^\p{L}{0,2}\d{4}$

This works for me:
^[a-zA-Z]{0,2}\d{4}$

Related

Regex Group Optional

I have the following regular expression that isn't working the way I thought it would.
("^\\d{2}(?:\\d{2})?\\.\\d{2}(\\.\\d{2-4})?$");
I am trying to match a string that starts with either 2 or 4 digits, followed by a period, followed by 2 digits and then optionally another period and either 2 or 4 digits.
I would expect 33.44.4444 to work, as would 33.33 but anytime I have a string that has a 2nd period, my expression fails.
What am I doing wrong ?
Your regex is correct for what you want to do except for the {2-4} part, if you use {2,4} it will go for the 2 to 4 characters capture you're looking for.
("^\\d{2}(?:\\d{2})?\\.\\d{2}(\\.\\d{2,4})?$");
Hope it helps.
As others have pointed out the syntax {2-4} is incorrect. Use {2,4} to specify a range of occurrences. But also if you only want 2 or 4 (not 3) I would use this regex:
#"^(\d{2}|\d{4})\.\d{2}(\.(\d{2}|\d{4}))?$"
The way you expressed "either two or four digits" in the first section of your expression is correct:
\\d{2}(?:\\d{2})?
The second part does it incorrectly:
(\\.\\d{2-4})?
Copy the first part into the second to fix the problem:
("^\\d{2}(?:\\d{2})?\\.\\d{2}(\\.\\d{2}(?:\\d{2})?)?$");
Demo.
You can use this regex:
^\d{2}(?:\d{2})?\.\d{2}(?:\.\d{2}(?:\d{2})?)?$
\d{2-4} will match {2-4} text literally.
RegEx Demo

RegEx to match 2 digits before the dot and one digit after the dot(just 0 or 5)

I need a regular expression that will match one or two digits before the dot and one digit after the dot (0 or 5).
I tried it out for at least one hour, and I'm getting mad....
Possible results should be:
5,0
5,5
30,0
30,5 etc.
If just one digit is insert, it want a result as follows :
5 --> 5,0
Is there someone who can help me? Thanks a lot!!
You just want to check for one or two digits, followed by the dot literal, and either '0' or '5'.
^\d{1,2}\.[05]$
That doesn't handle the single digit one, though. There's not an easy way to just match a single digit in the same regex you're matching one or two, so you could use a second regex:
^\d$
Then convert that to a double/float if you get any matches.
Try the following regex...
(?:\d{1,2}(?=.)|(?<=.)[50])
string regex=#"(?<b>\d{1,2})(?<a>[.]0|[.]5)?";
Match m=Regex.Match(input,regex);
string result=m.Groups["b"].Value+","+m.Groups["a"].Value==""?0:m.Groups["a"].Value;
Above code would give these results for input
550.57 => 50,5
644 => 44,0

regular expression in c#

I am trying to validate a texbox to allow numbers and letter(s) but not letters alone only e.g. 13492M
I am using C# regular expressions.
^[A-Za-z]*\d[A-Za-z\d]*$ should do it. (Possibly some letters, then a digit, then any more letters or digits.)
(Edited to add start/end matches.)
use maskedTextBox, it use a property "mask" to validate with the Expression that you want. So you only add the RegEx to your maskedTextBox and you don't have to validate everytime in your code (it will check against your RegEx automatically)
How about this:
([0-9]+[a-zA-Z]+ | [a-zA-Z]+[0-9]+)[a-zA-Z0-9]*
(Numbers first and then alphabets OR Alphabets first then numbers) atleast once or more then both alphabets and numbers which is optional
This Regex should work fine:
^[A-Za-z]*[0-9]+[A-Za-z]*$
This regex will allow numbers or letters+numbers. Just letters will fail.
Simply,
Pattern = "^[a-zA-Z0-9]*[0-9]+[a-zA-Z0-9]*$"
Details :
Start. ^
Zero or more mixed alphanumerical. [a-zA-Z0-9]*
One or more numerical. [0-9]+
Zero or more mixed alphanumerical. [a-zA-Z0-9]*
End. $

Regex for 1 or 2 digits, optional non-alphanumeric, 2 known alphas

I've been bashing my head against the wall trying to do what should be a simple regex - I need to match, eg 12po where the 12 part could be one or two digits, then an optional non-alphanumeric like a :.-,_ etc, then the string po.
The eventual use is going to be in C# but I'd like it to work in regular grep on the command line as well. I haven't got access to C#, which doesn't help.
^[0-9]{1,2}[:.,-]?po$
Add any other allowable non-alphanumeric characters to the middle brackets to allow them to be parsed as well.
^\d{1,2}[\W_]?po$
\d defines a number and {1,2} means 1 or two of the expression before, \W defines a non word character.
^[0-9][0-9]?[^A-Za-z0-9]?po$
You can test it here: http://www.regextester.com/
To use this in C#,
Regex r = new Regex(#"^[0-9][0-9]?[^A-Za-z0-9]?po$");
if (r.Match(someText).Success) {
//Do Something
}
Remember, # is a useful symbol that means the parser takes the string literally (eg, you don't need to write \\ for one backslash)

c# Regex question: only letters, numbers and a dot (2 to 20 chars) allowed

i am wrestling with my regex.
I want to allow only letters and numbers and a dot in a username, and 2 to 20 chars long
I thought of something like this
[0-9a-zA-Z]{2,20}
but then 21 chars is also ok, and that's not what i want
I suggest that you make two checks -- one for length and one for content based on the fact that you probably only want one dot in the name, rather than any number of dots. I'll assume that names like username and user.name are the only formats allowed.
This should get the content( but allows underscores as well):
^\w+(\.\w+)?$
If you don't want underscores, then you would use [0-9a-zA-Z]+ in place of \w+. To explain, it will match any string that consists of one or more word characters, followed by exactly 0 or 1 of a dot followed by one or more word characters. It must also match the beginning and end of the string, i.e., no other characters are allowed in the string.
Then you only need to get the length with a simple length check.
^[0-9a-zA-Z\.]{2,20}$
Try ^[\w\.]{2,20}$ instead.
You need to use start and end of string (^ and $), and escape the .:
^[0-9a-zA-Z\.]{2,20}$

Categories