Regular Expression doesn't Match with string - c#

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.

Related

Regex Expression - L.LL.#

I like to create regex expression that would check a string to make sure it starts with:
L.LL.#
Anything after this first number is an irrelevance. Is regex the best approach to solving this problem?
Note:
The L implies a letter and the # implies any number.
Use Regex.IsMatch(input, "^[a-zA-Z]\\.[a-zA-Z]{2}\\.\\d")
Assuming L represents a letter in the range A-Z or a-Z

Regular expression take correct number of brackets

\w+\(([^\)]*)?\)+
This regex will match
abc(1,3,abs(4)
foo(1,3,abs(4)))
I want to match only
abc(1,3,abs(4))
Is it possible?
You can use the following regex:
^[a-z]+\((?>[^()]+|\((?<DEPTH>)|\)(?<-DEPTH>))*(?(DEPTH)(?!))\)$
It will match any string of characters from a to z in the beginning, and then a matching number of parentheses and everything inside them.
A demo on regexstorm
Tested in Expresso:

regular expression start (^) does not work correctly

I want to match a pattern like 091\d{8} in a content.
I want to extract strings that start with 091, I try this:
^(091)\d{8}
this pattern only match when string begins in new line,what pattern must I use?
You should match for a word boundary (\b)
^ will only match the number if the string starts with 091, not in between.
You should match word boundaries in your regular expression ,
else it will fetch those expressions too which start with 091, but have more than 8 digits after that.
See this regex \b((091)\d{8})\b working at : http://regexr.com?310ra
The caputred group in parenthesis will give you the required number.

Need help in building regular expression

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

Regular Expression that Matches Any Number or Letter or Dash

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.

Categories