Regex .NET OR operator not working on alternative group - asp.net

I need a regex which validate string of numbers either math "aabb" or "abba" pattern.
For example: both 1122 or 1221 is valid
Regex for both "aabb", "abba" worked fine alone.
But when i'm trying to combine "aabb" OR "abba", the result of "aabb" is always false.
(1122 returned not valid)
Here is my implementation in C#:
string phoneNumber = "1221"; // "1122" failed
Dictionary<string, string> subPatterns = new Dictionary<string, string>();
subPatterns[#"(\d)(\d)\2\1$"] = "abba";
subPatterns[#"(\d)\1(\d)\2$"] = "aabb";
string pattern = string.Join("|", subPatterns.Select(e => e.Key));
foreach (Match m in Regex.Matches(phoneNumber, pattern))
{
if (m.Success)
{
Console.WriteLine("TRUE");
}
}
Did i missed something?

The alternation changes the capture group numbers. You can either account for the incremented numbers in the alternation:
subPatterns[#"(\d)(\d)\2\1$"] = "abba";
subPatterns[#"(\d)\3(\d)\4$"] = "aabb";
The pattern will look like this, matching the 4 digits at the end of the string due to the $
(\d)(\d)\2\1$|(\d)\3(\d)\4$
Or you can use the same named backreferences:
subPatterns[#"(?<1>\d)\k<1>(?<2>\d)\k<2>"] = "abba";
subPatterns[#"(?<1>\d)(?<2>\d)\k<2>\k<1>"] = "aabb";
The pattern will then look like
(?<1>\d)(?<2>\d)\k<2>\k<1>|(?<1>\d)\k<1>(?<2>\d)\k<2>
Note that if the matches are for the whole line, you can append an anchor ^ to it and the whole pattern will look like
^(?:(?<1>\d)(?<2>\d)\k<2>\k<1>|(?<1>\d)\k<1>(?<2>\d)\k<2>)$
See a regex demo and a C# demo.

Related

How to get numbers from a string in Asp.net VB?

This is my string "search=;pageid=62,67;categoryid=0;orderby=;showon=1" I want to get 62 and 67 separately, how can I do this?
The best way is to use RegEx. You can use this expression to get the optimized result:
(\d{2})
This RegEx finds all the numbers with 2 digits only.
You can use String.Split and a Lookup<TKey, TValue>:
var yourString = " search=;pageid=62,67;categoryid=0;orderby=;showon=1";
var lookup = yourString.Trim().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(t => t.Split('='))
.ToLookup(arr => arr[0], arr => arr[1].Split(','));
string[] allPageIDs = lookup["pageid"].FirstOrDefault();
// allPageIDs can be null if the string didn't contain pageid
foreach (string id in allPageIDs)
Console.WriteLine(id);
A lookup is similar to a dictionary but it if the input sequence contains the key multiple times no exception is raised but the value contains all results. Therefore FirstOrDefault above returns the first result which is a string[] with two values 62,67 in this case.

Find word (not containing substrings) in comma separated string

I'm using a linq query where i do something liike this:
viewModel.REGISTRATIONGRPS = (From a In db.TABLEA
Select New SubViewModel With {
.SOMEVALUE1 = a.SOMEVALUE1,
...
...
.SOMEVALUE2 = If(commaseparatedstring.Contains(a.SOMEVALUE1), True, False)
}).ToList()
Now my Problem is that this does'n search for words but for substrings so for example:
commaseparatedstring = "EWM,KI,KP"
SOMEVALUE1 = "EW"
It returns true because it's contained in EWM?
What i would need is to find words (not containing substrings) in the comma separated string!
Option 1: Regular Expressions
Regex.IsMatch(commaseparatedstring, #"\b" + Regex.Escape(a.SOMEVALUE1) + #"\b")
The \b parts are called "word boundaries" and tell the regex engine that you are looking for a "full word". The Regex.Escape(...) ensures that the regex engine will not try to interpret "special characters" in the text you are trying to match. For example, if you are trying to match "one+two", the Regex.Escape method will return "one\+two".
Also, be sure to include the System.Text.RegularExpressions at the top of your code file.
See Regex.IsMatch Method (String, String) on MSDN for more information.
Option 2: Split the String
You could also try splitting the string which would be a bit simpler, though probably less efficient.
commaseparatedstring.Split(new Char[] { ',' }).Contains( a.SOMEVALUE1 )
what about:
- separating the commaseparatedstring by comma
- calling equals() on each substring instead of contains() on whole thing?
.SOMEVALUE2 = If(commaseparatedstring.Split(',').Contains(a.SOMEVALUE1), True, False)

ASP.net validator regular expression and accented names / characters

I have a asp.net control that is using a regular expression to validate the users input for first name and last name. It works for up to 40 characters...and I think by the looks of the expression it also allows ' for names like O'Donald and maybe hypenated names too.
ValidationExpression="^[a-zA-Z''-'\s]{1,40}$"
My problem is with accented names/characters e.g. Spanish and French names that may contain for example ñ are not allowed. Does anyone know how to modify my expression to take this into account?
You want
\p{L}: any kind of letter from any language.
From regular-expressions.info
\p{L} or \pL is every character in the unicode table that has the property "letter". So it will match every letter from the unicode table.
You can use this within your character class like this
ValidationExpression="^[\p{L}''-'\s]{1,40}$"
Working C# test:
String[] words = { "O'Conner", "Smith", "Müller", "fooñ", "Fooobar12" };
foreach (String s in words) {
Match word = Regex.Match(s, #"
^ # Match the start of the string
[\p{L}''-'\s]{1,40}
$ # Match the end of the string
", RegexOptions.IgnorePatternWhitespace);
if (word.Success) {
Console.WriteLine(s + ": valid");
}
else {
Console.WriteLine(s + ": invalid");
}
}
Console.ReadLine();

What is the regular expression for "No quotes in a string"?

I am trying to write a regular expression that doesn't allow single or double quotes in a string (could be single line or multiline string). Based on my last question, I wrote like this ^(?:(?!"|').)*$, but it is not working. Really appreciate if anybody could help me out here.
Just use a character class that excludes quotes:
^[^'"]*$
(Within the [] character class specifier, the ^ prefix inverts the specification, so [^'"] means any character that isn't a ' or ".)
Just use a regex that matches for quotes, and then negate the match result:
var regex = new Regex("\"|'");
bool noQuotes = !regex.IsMatch("My string without quotes");
Try this:
string myStr = "foo'baa";
bool HasQuotes = myStr.Contains("'") || myStr.Contains("\""); //faster solution , I think.
bool HasQuotes2 = Regex.IsMatch(myStr, "['\"]");
if (!HasQuotes)
{
//not has quotes..
}
This regular expression below, allows alphanumeric and all special characters except quotes(' and "")
#"^[a-zA-Z-0-9~+:;,/#&_#*%$!()\[\] ]*$"
You can use it like
[RegularExpression(#"^[a-zA-Z-0-9~+:;,/#&_#*%$!()**\[\]** ]*$", ErrorMessage = "Should not allow quotes")]
here use escape sequence() for []. Since its not showing in this post

How to delete part of a string in actionscript?

So my string is something like "BlaBlaBlaDDDaaa2aaa345" I want to get rid of its sub string which is "BlaBlaBlaDDD" so the result of operation will be a string "aaa2aaa345" How to perform such thing with actionscript?
I'd just use the String#replace method with a regular expression:
var string:String = "BlaBlaBlaDDD12345";
var newString:String = string.replace(/[a-zA-Z]+/, ""); // "12345"
That would remove all word characters. If you're looking for more complex regular expressions, I'd mess around with the online Rubular regular expression tester.
This would remove all non-digit characters:
var newString:String = string.replace(/[^\d]+/, ""); // "12345"
If you know the exact string you want to remove, then just do this:
var newString:String = string.replace("BlaBlaBlaDDD", "");
If you have a list (array) of substrings you want to remove, just loop through them and call the string.replace method for each.

Resources