Regular Expression that Matches Any Number or Letter or Dash - c#

Given searchString = "23423asdfa-''"
This regular expression should evaluate to false but it does not! Any ideas?
Regex rgx = new Regex(#"[\w-]*");
rgx.IsMatch(searchString)

It's because you haven't constrained it to match the entire string. Hence it is allowed to consider matches on subsets of the string. A very large subset of the string matches the data hence the regex returns true.
Try the following to force it to match the entire input.
Regex rgx = new Regex(#"^[\w-]*$");
rgx.IsMatch(searchString)

You need to anchor your expression. If you don't, then if any substring of the input matches, the regex match is considered successful. Change the regex to "^[\w-]*$" where the ^ and $ will match the beginning and end of the string, respectively.

Related

Regular expression matching c# without some tags

I want to match exact and prefix wildcard match but there's one condition that It should not be surrounded by a particular tag.
For example: if the word to match is test, the regular expression should match
test, testing,tester ,testing.aspx but it should not match test</x> and testing</x>, tester</x> and other words with prefix test
I came up with a regex which is matching test</x> too.
string regex = string.Format("\\b{0}(\\S)*(?!</x>)", "test");
Can somebody help me in correcting my regex?
The \btest(\S)*(?!</x>) pattern matches test</x> because \btest finds a word starting with test, then matches and repeatedly captures any 0+ non-whitespace chars, and then checks if there is no </x> immediately to the right of the current location. Since (\S)* matches the whole </x> at once the negative lookahead checks for </x> when the regex index is already placed after this </x> - and thus it returns true and the match is a success.
Yo may use
string regex = string.Format(#"(?>\b{0}[^<\s]*)(?!</x>)", "test");
// or, beginning with C#6
// var regex = $#"(?>\b{SearchWord}[^<\s]*)(?!</x>)";
See the regex demo
Now, it will match like this:
(?>\btest[^<\s]*) - an atomic group matching
\b - a word boundary
test - search term
[^<\s]* - 0+ chars other than < and whitespace
(?!</x>) - a negative lookahead that fails the match if there is a </x> char sequence immediately to the right of the current location

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.

How do I exclude a regex value in a replace

I have a regex expression which searches for strings using a Prefix and a Suffix. In it's simplest form \$\$\w+\$\$ will find $$My_Name$$ (in this case the Prefix and Suffix are both equal to $$) Another example would be \[\#\w+\#\] to match [#My_Name#].
The Prefix and Suffix will always be a specific string of 0 to n characters which I can always safely escape for a direct character match.
I extract the Matches in my C# code so I can work with them but obviously my match contains $$My_Name$$ but what I want is to simply get the inner string between the Suffix and Prefix: My_Name.
How do I exclude the Prefix and Suffix from the result?
Change your REGEX to \$\$(\w+)\$\$ and use $1 to get the matching (inner) string.
For example
string pattern = #"\$\$(\w+)\$\$";
string input = "$$My_Name$$";
Regex rgx = new Regex(pattern);
Match result = rgx.Match(input);
Console.WriteLine(result.Groups[1]);
Outputs: "My Name"
P.S - There's no need to use explicitly typed local variables, but I just wanted the types to be clear.
You can group your w+ into a group like this (w+) then when you retrieve the matched string you might be able to ask for that subgroup.
I do not know if I am wrong (but you didn't provided any code whatsoever) but I think this is how it is done: .Groups[1].Value on the the result of Regex.Match.
How about the regex below. It works by capturing the first character into a named group then capturing any repeats into a named group called first group which it then uses to match the end of the string. It will work with any number of repeated character so long as they repeated at the end of the word.
'(?<first_group>(?<first_char>.)\k<first_char>+)(?<word>\w+)\k<first_group>+'
You just need to then extract the capture group named word like so:
String sample = "$$My_Name$$";
Regex regex = new Regex("(?<first_group>(?<first_char>.)\k<first_char>+)(?<word>\w+)\k<first_group>+");
Match match = regex.Match(sample);
if (match.Success)
{
Console.WriteLine(match.Groups["word"].Value);
}
You can use named group like this:
(\$\$)(?<group1>.+?)\1 -- pattern 1 (first case)
\[(#)(?<group2>.+?)\1\] -- pattern 2 (second case)
or combined representation would be:
(\$\$)(?<group1>.+?)\1|\[(#)(?<group2>.+?)\3\]
I would suggest you to use .+? it will help you to match any character other than your prefix/suffix.
Live Demo

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.

How to check if a Regex expression matches an entire string in c#?

I am new to regex expressions so sorry if this is a really noob question.
I have a regex expression... What I want to do is check if a string matches the regex expression in its entirety without the regex expression matching any subsets of the string.
For example...
If my regex expression is looking for a match of \sA\s*, it should return a match if the string it is comparing it to is " A " but if it compares to the string " A B" it should not return a match.
Any help would be appreciated? I code in C#.
You would normally use the start end end anchors ^ and $ respecitvely:
^\s*A*\s*$
Keep in mind that, if you regex engine supports multi-line, this may also capture strings that span multiple lines as long as one of those lines matches the regex(since ^ then anchors after any newline or string-start and $ before any newline or string end). If you're only running the regex against a single line, that won't be a problem.
If you want to ensure that a multi-line input is only a single line consisting of your pattern, you can use \A and \Z if supported - these mean start and end of string regardless of newlines.
If you cannot or don't want to change the regular expression, then you can also use:
var match = regex.Match(pattern);
if (match.Success && match.Length == pattern.Length)
{
// TODO: Entire string was matched, and not a sub string
}

Categories