Need help in building regular expression - c#

What is the Regular expression for
OD
-0D
-123D
-145Y
234w
and not +234D or -678m etc.
I do have
string EXP_REGEX_VALID_LITERAL = #"[0-9]*[d|D|w|W|m|M|q|Q|y|Y]";
Regex regex = new Regex(EXP_REGEX_VALID_LITERAL);
return regex.IsMatch(inputString);
but it is failing for "/0345d"
Validation Rules:
the expression is a alphanumeric one(where the alphabets are optional) where the alphabets can be only d|D|w|W|m|M|q|Q|y|Y and can appear only after the numerals. Also if any character can come before any numerals that can only be a minus.so -123 is valid or -123d or 123d or 123w are valid. but not 23dw or +12d etc.
Thanks

If I understand well what you want, may be this will work:
/^-?\d+[dwmqy]$/i
c# syntax (not quite sure):
Regex.Match("/0123d", "^-?\d+[dwmqy]$",RegexOptions.IgnoreCase);
Where:
^ begining of the string
-? optionnal -
\d+ one or more digits
[dwmqy] one of these char
$ end of the string
i case insensitive

Related

Regular Expression doesn't Match with string

I am trying to use Regular Expressions to find a string sequence inside a string.
The pattern i am looking for is:
dd.dd.dddd dd:dd:dd //d is a digit from 0-9
my regex is:
Regex r = new Regex(#"(\d[0-9]{2}.\d[0-9]{2}.\d[0-9]{4}\s\d[0-9]{2}:\d[0-9]{2}:\d[0-9]{2})$");
I am now trying to check, if the string "27.11.2014 09:14:59" is Matching to the regex, but sadly it isn't matching.
string str= "27.11.2014 09:14:59";
Regex r = new Regex(#"(\d[0-9]{2}.\d[0-9]{2}.\d[0-9]{4}\s\d[0-9]{2}:\d[0-9]{2}:\d[0-9]{2})$");
test = r.IsMatch(str,0);
//output: test=false
Anyone knows why the String is not Matching with that regular expression?
\d[0-9]{2} matches three digits:
\d first digit
[0-9] second digit
{2} causes the previous expression ([0-9]) to match again
If you remove all occurences of \d, your pattern should work. You should escape all dots . though, because right now they match any character, not just a ..
As Rawing already said, the upper Regular expression is trying to match 3 digits instead of one. for everyone who want to know how the regular expression should look like:
#"(\d{2}.\d{2}.\d{4}\s\d{2}:\d{2}:\d{2})$"
Thats working, at least for me.

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}$)";

Validate Unicode Length With Regex

How can I validate ۱۳۹۱/۰۹/۰۹ string with Regex
I want the length of each separate slash be exact as {4}/{2}/{2}
the Unicode range is [\u06F0-\u06F9].
I have problem with length checking.
You can use the following regular expression:
"^[\u06F0-\u06F9]{4}/[\u06F0-\u06F9]{2}/[\u06F0-\u06F9]{2}$"
You're probably missing the ^ to make it start the match at the beginning of the string and the $ to make it end the match at the end of the string. Without these changes strings that were longer, but that contained your expression would yield as a match.
With this change a match is only successful if the string contains your pattern and does not have any extra characters to the left or to the right of the target pattern.
This regex should work for you:
"(^|[^\u06F0-\u06F9]{1})[\u06F0-\u06F9]{4}/[\u06F0-\u06F9]{2}/[\u06F0-\u06F9]{2}([^\u06F0-\u06F9]{1}|$)"
Match the date expression under both of the following conditions:
Condition1: It should be either at the beginning of the string or after a single character that's not in the character range [\u06F0-\u06F9]
Condition2: It should be either at the end of the string or before a single character that's not in the character range [\u06F0-\u06F9]
This will not match the expression in this string:
How can I validate ۱۱۳۹۱/۰۹/۰۹ string with Regex
-------------------^5Numbers, not matched
Or this string:
How can I validate ۱۱۳۹۱/۰۹/۰۹۹ string with Regex
------------------------------^Three numbers, not matched
but still will match the date expression in this string:
How can I validate۱۳۹۱/۰۹/۰۹string with Regex
------------------^---------^ No whitespaces above ^, the expression is matched though
If you want to avoid this, i.e, just match the date expression alone, with whitespaces (and linebreaks) before and after it, use the following Regex:
(^|[ \t\n]{1})[\u06F0-\u06F9]{4}/[\u06F0-\u06F9]{2}/[\u06F0-\u06F9]{2}([ \t\n]{1}|$)
Hope that's helpful.

C# regex to match "+99.99" or "-99.99" or "99.99"

I am looking for a regular expression in C# to match any of these strings: "+99.99", "-99.99", "99.99". The same regular expression should not match the string "+-99.99". Can any one please suggest an answer.
The following worked for me:
String regex = "^(\\+|-)?99\\.99$";
String str1 = "-99.99";
String str2 = "+99.99";
String str3 = "99.99";
String str4 = "+-99.99";
System.Console.WriteLine(Regex.IsMatch(str1, regex));
System.Console.WriteLine(Regex.IsMatch(str2, regex));
System.Console.WriteLine(Regex.IsMatch(str3, regex));
System.Console.WriteLine(Regex.IsMatch(str4, regex));
System.Console.ReadKey();
Yielded:
True True True False
Explanation: ^ will instruct the regex engine to start matching from the beginning of the string, (\\+|-) denotes a + or - character. The + is a special character in regex syntax and thus needs to be escaped. The OR operator is denoted by the | character.
The ? denotes that the + or - character may or may not be there (it will match 0 or 1 instances of whatever preceeds it).
99\\.99 denotes the string 99.99. The . is also a special character in regex syntax and thus needs to be escaped. The $ character will instruct the regex engine to stop matching at the end of the string.
Try this one:
[+-]?\d+\.\d+
explain:
[+-]? any character of: '+', '-'
(optional, matching the most amount possible)
\d+ digits (0-9)
(1 or more time, matching the most amount possible)
\. '.'
\d+ digits (0-9)
(1 or more times, matching the most amount possible)
You can test your regex with some online testers (very helpful to learn regex).
My favorites are : http://www.regexplanet.com/advanced/dotnet/index.html and http://regexhero.net/tester/
If you want it to only match positive or negative 99.99, you can use [+-]?99\.99. This regular expression will match 99.99, optionally preceded by a + or - character. If you want to match any number, optionally with up to two decimal places, you could use [+-]?\d{1,}(\.\d{0,2})?.
As usual, if you want the regular expression to match only the entire string, you need to prepend ^ and append $, i.e. ^[+-]?99\.99$.

Regular Expression to validate email ending in .edu

I am trying to create a regex validation attribute in asp.net mvc to validate that an entered email has the .edu TLD.
I have tried the following but the expression never validates to true...
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+edu
and
\w.\w#{1,1}\w[.\w]?.edu
Can anyone provide some insight?
This should work for you:
^[a-zA-Z0-9._%+-]+#[a-zA-Z0-9.+-]+\.edu$
Breakdown since you said you were weak at RegEx:
^ Beginning of string
[a-zA-Z0-9._%+-]+ one or more letters, numbers, dots, underscores, percent-signs, plus-signs or dashes
# #
[a-zA-Z0-9.+-]+ one or more letters, numbers, dots, plus-signs or dashes
\.edu .edu
$ End of string
if you're using asp.net mvc validation attributes, your regular expression actually has to be coded with javascript regex syntax, and not c# regex syntax. Some symbols are the same, but you have to be weary about that.
You want your attribute to look like the following:
[RegularExpression(#"([0-9]|[a-z]|[A-Z])+#([0-9]|[a-z]|[A-Z])+\.edu$", ErrorMessage = "text to display to user")]
the reason you include the # before the string is to make a literal string, because I believe c# will apply its own escape sequences before it passes it to the regex
(a|b|c) matches either an 'a' or 'b' or 'c'. [a-z] matches all characters between a and z, and the similar for capital letters and numerals so, ([0-9]|[a-z]|[A-Z]) matches any alphanumeric character
([0-9]|[a-z]|[A-Z])+ matches 1 or more alphanumeric characters. + in a regular expression means 1 or more of the previous
# is for the '#' symbol in an email address. If it doesn't work, you might have to escape it, but i don't know of any special meaning for # in a javascript regex
Let's simplify it more
[RegularExpression(#"\w+#\w+\.edu$", ErrorMessage = "text to display to user")]
\w stands for any alphanumeric character including underscore
read some regex documentation at https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions for more information
You may have different combinations and may be this very simple one :
\S+#\S+\.\S+\.edu
try this:
Regex regex = new Regex(#"^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.(edu)$", RegexOptions.IgnoreCase);
ANSWER UPDATED...

Categories