regular expression in c# - 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. $

Related

Regex expression with decimals, one letter and a question mark

I'm trying to make a suvat calculator so one can input decimals, a letter (e.g., S) and a question mark if you do not have a value.
Tests that will be valid include "2.3", "S", "?" but not values like "2.5s", "??", etc (only one type, can't have decimals AND a letter in the same input box)
Is there a regex expression for this? So far I have only got the regex for the decimal number:
^[0-9]\\d*(\\.\\d+)
I did also try a way simpler one but I would like a more developed expression for later on.
[0-9sS.?]
if i got your use case right, then this might work:
^(\?|(\d+\.?\d+)|\S)$
Read it as: The word contains either one question mark,
or a numeric value with propably a dot and numbers behind that
or a single letter
You can try it our here:
https://regex101.com/r/wLGJhJ/1
You can use
#"^(?:[0-9]+(?:\.[0-9]+)?|[A-Za-z?])\z"
Details:
^ - start of string
(?: - start of a non-capturing group:
[0-9]+ - one or more ASCII digits
(?:\.[0-9]+)? - an optional occurrence of . and one or more ASCII digits
| - or
[A-Za-z?] - an ASCII letter or ?char
) - end of the group
\z - the very end of string.
See a .NET regex demo online.

Regular expression for SSN that accept one extra character

I am looking for something for Social Security Number which is in the form "###-##-####". I need a way that the first character can also be allowed to type "#"
How do I add that? I need it for a masked text box mask.
Try this regex:
^(#|\d\d\d-\d\d-\d\d\d\d)$
(Note: this is with the US format: ###-##-####)
The ^ and $ mean the "start" and "end" of the string, so that you can't match items in the middle of your text.
The | says "one or the other". So it will match a #, or the digits.
The following will match
123-45-6789
#
but this won't match
234-3333-14234
#123-45-6789
You can take a demo here.
Make sure when you type this into c# you use the correct character escaping:
string pattern = #"^(#|\d\d\d-\d\d-\d\d\d\d)$";
all you need is this (\d is any digit and \d is \d escaped for c# and #? means it will accept 0 or 1 #)
#?\\d\\d\\d-\\d\\d-\\d\\d\\d\\d

Regular expression in C# for Number with Decimal Point & Commas

I need to enter amount in a textbox which allows numbers with decimal point and commas.
What is the regular expression for this?
I used the below
txtInitialAmountGuarDetails.ValidationSettings.RegularExpression.ValidationExpression
= #"^[-+]?\d*[0-9](|.\d*[0-9])(|,\d*[0-9])?$";
But it not working for large numbers like 300,000,000,000,000.
Build it up piecemeal. Given a US locale, a number with these rules has in order:
The string beginning: ^
An optional sign: [+-]?
Up to 3 digits: \d{1,3}
A comma followed by 3 digits, repeated any number of times: (?:,\d{3})*
An optional decimal point and decimal part: (?:[.]\d+)?
The string end: $
Do you have restrictions on the number of digits after the decimal point? Then change the last plus sign to {2} for 2 digits.
So, the regex is:
#"^[+-]?\d{1,3}(?:,\d{3})*(?:[.]\d+)?$"
Or, if you want to explain your work, use the x option and:
#"(?x) # Extended format.
^[+-]? # Optional sign.
\d{1,3} # Initial 1-3 digits.
(?:,\d{3})* # Any number of commas followed by 3 digits.
(?:[.]\d+)?$" # An optional decimal point followed by any number of digits.
But does C# have a locale-dependent validator already?
I have not run it, but you can try it out.
var regexp =/^\s*?([\d\,]+(\.\d{1,2})?|\.\d{1,2})\s*$/;
This works: \d{1,3}(,\d{3})*\.{0,1}(\d{3},)*\d{0,3}
As for the after the comma issue, any choice should be fine. If you go with commas, my regex works. If you do 5 digits then a space just replace the end with (\d{5}\s{1})*\d{0,5}. And ofcourse if you just dont use any deliminator after the decimal you just put \d*
You can try this regex too:
^([+-]?\d{1,3}(?:,\d{1,3})*(?:\.\d+)*)$
Keep in mind . has a specific meaning in regex engine so it is necessary to escape it.
I would also suggest you to not use regex for this task instead look at masked textbox.
try this one:
^([0-9]{3}[,.]|)+[0-9]{0,3}$
let me know if it needs any enhancements...

Regular expressions to extract punctuations/characters

Hi guys I need a regex that only extracts punctuations/characters.
I have this so far :
[._^%$#!~#,-]+
but this works if there is at least 1 punctuations and still allows for any other char (digit or letter)
I need to to only allow punctuations/characters
Try this:
^[\p{S}\p{P}]+$
\p{S} matches any symbol character, and \p{P} matches any punctuation.
Note that your pattern will not match all the symbols and punctuations not present in the list.
Try anchoring the regex to the start and the end of the string (unless you're using Multiline matching) - i.e. ^ at the beginning and $ at the end:
^[._^%$#!~#,-]+$
Note - this does not endorse your actual pattern (I can't say whether this is matching all the 'special characters' you're talking about, but it will make it so that the entire string must be all 'special'.
[^a-zA-Z0-9]* u can try something like this. Should NOT accept those chars, cba to writte all beside chars beside one u typed.
\W
Matches any character that is not a word character (alphanumeric & underscore).

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

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}$

Categories