Regex expression with decimals, one letter and a question mark - c#

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.

Related

Regex to match if string is exactly as defined

How can I check that the string is in correct format. I want the string to compare and pass only if matches exactly. Following are the correct formats :
0.#
0.##
0.###
0.####
0.#####
The hash (#) after the dot (.) can be upto 10 characters but it should only have 0.# nothing else is allowed.
Can someone please guide me how can I validate a string of this type ?
Im Regular Expression the carret (^) represent start-of-line and the ($) represents end-of-line (or before newline).
A regex with an exact match is just what you want enclosed by ^ and $. But you must ensure that special regular expression characters are quoted. For example the regex
^Hello World$
would match exactly on the String "Hello World" and nothing else.
You also can use numbers directly. You need to escape the dot "." as a dot in a regular expression means any character except newline. You escape a character by adding a backslash.
Next you should know about quantifiers. The usually ones are
-> 0 or many
-> 1 or many
{n} -> exactly n times
{n,} -> at least n times
{n,m} -> n to m times
So you can write:
^0\.#{1,10}$
If you use a normal string in C# with quotations (") you must use two backslashes
^0\\.#{1,10}$

Regex for Name, Streetname, Cityname, etc

I m programming a web application with asp.net mvc and c#.
In a form a user should enter a name, a streetname and a city in different fields.
Start: The entered value has to start with an 'alphabetic' character (no matter if the language is english, chinese or french or what ever. So things like é and chinese chars and so on are ok. But chars like *¥##1 and so on are not allowed)
Middle: The same as i said first and spaces (but not two spaces after eachother).
End: That what I have said for the start.
This is correct:
A b c
Abcd ef
Abcdef
This is not correct:
1abc
A1 bc
1 2 3
a b c (space at the start)
Question:
What is the correct regex for this?
How can i set the length?
In a second case I want to allow numbers 0123456789 too (like the chars)
This is what I have: '^[a-zA-Z][a-zA-Z ][a-zA-Z]$'
Thank you
You want to validate strings that only contain letter words separated with a single space between them.
You may use a regex like
^\p{L}+(?: \p{L}+)*$
Or, if any whitepsace is allowed:
^\p{L}+(?:\s\p{L}+)*$
See the regex demo
To make it only match strings of 3 or more chars, use
^(?=.{3})\p{L}+(?:\s\p{L}+)*$
^^^^^^^^
Details
^ - start of a string
(?=.{3}) = a positive lookahead that requires any 3 chars immediately after the start of a string
\p{L}+ - 1 or more any Unicode letters
(?:\s\p{L}+)* - zero or more repetitions of
\s - any whitespace
\p{L}+ - 1 or more any Unicode letters
$ - end of string
Note that if you need to use it in ASP.NET, only use this regex to validate on the server side, as on the client side, this pattern might not be correctly handled by JavaScript regex.
You can use this regex:
^(?:\p{L}+ )*\p{L}+$
\p{L} matches all unicode code points that are in the "Letters" category.
The regex matches 0 or more of \p{L}+ (one or more letters plus a space) and then ensures there is at least one or more letters.
Demo
Example code:
Console.WriteLine(Regex.IsMatch("abc def", #"^(?:\p{L}+ )*\p{L}+$"));

Regex for alpha number string in c# accepting underscore and white spaces

I already gone through many post on SO. I didn't find what I needed for my specific scenario.
I need a regex for alpha numeric string.
where following conditions should be matched
Valid string:
ameya123 (alphabets and numbers)
ameya (only alphabets)
AMeya12(Capital and normal alphabets and numbers)
Ameya_123 (alphabets and underscore and numbers)
Ameya_ 123 (alphabets underscore and white speces)
Invalid string:
123 (only numbers)
_ (only underscore)
(only space) (only white spaces)
any special charecter other than underscore
what i tried till now:
(?=.*[a-zA-Z])(?=.*[0-9]*[\s]*[_]*)
the above regex is working in Regex online editor however not working in data annotation in c#
please suggest.
Based on your requirements and not your attempt, what you are in need of is this:
^(?!(?:\d+|_+| +)$)[\w ]+$
The negative lookahead looks for undesired matches to fail the whole process. Those are strings containing digits only, underscores only or spaces only. If they never happen we want to have a match for ^[\w ]+$ which is nearly the same as ^[a-zA-Z0-9_ ]+$.
See live demo here
Explanation:
^ Start of line / string
(?! Start of negative lookahead
(?: Start of non-capturing group
\d+ Match digits
| Or
_+ Match underscores
| Or
[ ]+ Match spaces
)$ End of non-capturing group immediately followed by end of line / string (none of previous matches should be found)
) End of negative lookahead
[\w ]+$ Match a character inside the character set up to end of input string
Note: \w is a shorthand for [a-zA-Z0-9_] unless u modifier is set.
One problem with your regex is that in annotations, the regex must match and consume the entire string input, while your pattern only contains lookarounds that do not consume any text.
You may use
^(?!\d+$)(?![_\s]+$)[A-Za-z0-9\s_]+$
See the regex demo. Note that \w (when used for a server-side validation, and thus parsed with the .NET regex engine) will also allow any Unicode letters, digits and some more stuff when validating on the server side, so I'd rather stick to [A-Za-z0-9_] to be consistent with both server- and client-side validation.
Details
^ - start of string (not necessary here, but good to have when debugging)
(?!\d+$) - a negative lookahead that fails the match if the whole string consists of digits
(?![_\s]+$) - a negative lookahead that fails the match if the whole string consists of underscores and/or whitespaces. NOTE: if you plan to only disallow ____ or " " like inputs, you need to split this lookahead into (?!_+$) and (?!\s+$))
[A-Za-z0-9\s_]+ - 1+ ASCII letters, digits, _ and whitespace chars
$ - end of string (not necessary here, but still good to have).
If I understand your requirements correctly, you need to match one or more letters (uppercase or lowercase), and possibly zero or more of digits, whitespace, or underscore. This implies the following pattern:
^[A-Za-z0-9\s_]*[A-Za-z][A-Za-z0-9\s_]*$
Demo
In the demo, I have replaced \s with \t \r, because \s was matching across all lines.
Unlike the answers given by #revo and #wiktor, I don't have a fancy looking explanation to the regex. I am beautiful even without my makeup on. Honestly, if you don't understand the pattern I gave, you might want to review a good regex tutorial.
This simple RegEx should do it:
[a-zA-Z]+[0-9_ ]*
One or more Alphabet, followed by zero or more numbers, underscore and Space.
This one should be good:
[\w\s_]*[a-zA-Z]+[\w\s_]*

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 Expressions: Determining if a String is either a number or variable

I am trying to combine two Regular Expression patterns to determine if a String is either a double value or a variable. My restrictions are as follows:
The variable can only begin with an _ or alphabetical letter (A-Z, ignoring case), but it can be followed by zero or more _s, letters, or digits.
Here's what I have so far, but I can't get it to work properly.
String varPattern = #"[a-zA-Z_](?: [a-zA-Z_]|\d)*";
String doublePattern = #"(?: \d+\.\d* | \d*\.\d+ | \d+ ) (?: [eE][\+-]?\d+)?";
String pattern = String.Format("({0}) | ({1})",
varPattern, doublePattern);
Regex.IsMatch(word, varPattern, RegexOptions.IgnoreCase)
It seems that it is capturing both Regular Expression patterns, but I need it to be either/or.
For example, _A2 2 is valid using the code above, but _A2 is invalid.
Some examples of valid variables are as follows:
_X6 , _ , A , Z_2_A
And some examples of invalid variables are as follows:
2_X6 , $2 , T_2$
I guess I just need clarification on the pattern format for the Regular Expression. The format is unclear to me.
As noted, the literal whitespace you've put in your regular expressions is part of the regular expression. You won't get a match unless that same whitespace is in the text being scanned by the regular expression. If you want to use whitespace to make your regex, you'll need to specify RegexOptions.IgnorePatternWhitespace, after that, if you want to match any whitespace, you'll have to do so explicitly, either by specifying \s, \x20, etc.
It should be noted that if you do specify RegexOptions.IgnorePatternWhitespace, you can use Perl-style comments (# to end of line) to document your regular expression (as I've done below). For complex regular expressions, someone 5 years from now — who might be you! — will thank you for the kindness.
Your [presumably intended] patterns are also, I think, more complex than they need be. A regular expression to match the identifier rule you've specified is this:
[a-zA-Z_][a-zA-Z0-9_]*
Broken out into its constituent parts:
[a-zA-Z_] # match an upper- or lower-case letter or an underscore, followed by
[a-zA-Z0-9_]* # zero or more occurences of an upper- or lower-case letter, decimal digit or underscore
A regular expression to match the conventional style of a numeric/floating-point literal is this:
([+-]?[0-9]+)(\.[0-9]+)?([Ee][+-]?[0-9]+)?
Broken out into its constituent parts:
( # a mandatory group that is the integer portion of the value, consisting of
[+-]? # - an optional plus- or minus-sign, followed by
[0-9]+ # - one or more decimal digits
) # followed by
( # an optional group that is the fractional portion of the value, consisting of
\. # - a decimal point, followed by
[0-9]+ # - one or more decimal digits
)? # followed by,
( # an optional group, that is the exponent portion of the value, consisting of
[Ee] # - The upper- or lower-case letter 'E' indicating the start of the exponent, followed by
[+-]? # - an optional plus- or minus-sign, followed by
[0-9]+ # - one or more decimal digits.
)? # Easy!
Note: Some grammars differ as to whether the sign of the value is a unary operator or part
of the value and whether or not a leading + sign is allowed. Grammars also vary as to whether
something like 123245. is valid (e.g., is a decimal point with no fractional digits valid?)
To combine these two regular expression,
First, group each of them with parentheses (you might want to name the containing groups, as I've done):
(?<identifier>[a-zA-Z_][a-zA-Z0-9_]*)
(?<number>[+-]?[0-9]+)(\.[0-9]+)?([Ee][+-]?[0-9]+)?
Next, combine with the alternation operation, |:
(?<identifier>[a-zA-Z_][a-zA-Z0-9_]*)|(?<number>[+-]?[0-9]+)(\.[0-9]+)?([Ee][+-]?[0-9]+)?
Finally, enclose the whole shebang in an #"..." literal and you should be good to go.
That's about all there is to it.
Spaces are not ignored in regular expressions by default, so for each space in your current expressions it is looking for a space in that string. Add the RegexOptions.IgnorePatternWhitespace flag or remove the spaces from your expressions.
You will also want to add some beginning and end of string anchors (^ and $ respectively) so you do not match just part of a string.
You should avoid having spaces in your regular expressions unless you explicitly set IgnorePatterWhiteSpace. To make sure you get only matches on complete words you should include the beginning of line (^) and end of line ($) characters. I would also suggest you build the entire expression pattern instead of using String.Format("({0}) | ({1})", ...) as you have here.
The below should work given your examples:
string pattern = #"(?:^[a-zA-Z_][a-zA-Z_\d]*)|(?:^\d+(?:\.\d+){0,1}(?:[Ee][\+-]\d+){0,1}$)";

Categories