Regular expression take correct number of brackets - c#

\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:

Related

Having trouble matching the 17 characters after the first comma

I am trying to match 17 characters after the first comma. The string ends at the second comma. How do I match between these two commas using regex? Is there a way to match the string if it varies in length? Below is my test string.
0 PSC_OK,MACESBCE218002001,C07KTL89290003;1,C07KTL89290003,1;2,C07KTL89290003,0;3,C07KTL89290003,0;4,C07KTL89290003,0;5,C07KTL89290003,0;6,C07KTL89290003,0;7,C07KTL89290003,0;8,C07KTL89290003,0;9,C07KTL89290003,0;10,C07KTL89290003,0;11,C07KTL89290003,0;12,C07KTL89290003,0;13,C07KTL89290003,0;14,C07KTL89290003,0;15,C07KTL89290003,0;16,C07KTL89290003,0;17,C07KTL89290003,0;18,C07KTL89290003,0;19,C07KTL89290003,0;20,C07KTL89290003,0;21,C07KTL89290003,0;22,C07KTL89290003,0;23,C07KTL89290003,0;24,C07KTL89290003,0;25,C07KTL89290003,0;26,C07KTL89290003,0;27,C07KTL89290003,0;28,C07KTL89290003,0;29,C07KTL89290003,0;30,C07KTL89290003,0;31,C07KTL89290003,0;32,C07KTL89290003,0;33,C07KTL89290003,0;34,C07KTL89290003,0;35,C07KTL89290003,0;36,C07KTL89290003,0;37,C07KTL89290003,0;38,C07KTL89290003,0;39,C07KTL89290003,0;40,C07KTL89290003,0
Yes, you can build a regex expression like
/(,?)([a-zA-Z0-9;_]+)(,?)/gi
With Regex Expression you can get any match or group, the sitaxis change according to language, but, it almost the same.
And you can test it in page Regexr

Regex issue i am stuck with

I have to write a regex for matching a pattern 1-6/2011.
In this case, digits before the / can not be greater than 12.
So I have to select digits between 1-12.
I have written a regex:
^[1-9][0-2]?\s*[-−—]\s*[1-9][0-2]?\s*/\s*2[01][0-9][0-9]$
However, here I am getting 20-6/2014 also as a match.
I tried with a negative look-behind:
^[1-9](?<![2-9])[0-2]?\s*[-−—]\s*[1-9](?<![2-9])[0-2]?\s*/\s*2[01][0-9][0-9]$
Here, single digits are not getting identified.
You can use the following update of your regex:
^(?:0?[1-9]|1[0-2])\s*[-−—]\s*(?:0?[1-9]|1[0-2])\s*/\s*\s*2[01][0-9]{2}$
See demo
It will not match 12-30/2014, 12-31/2014, 12-32/2014, 13-31/2014, 20-6/2014.
It will match 1-6/2011 and 02-12/2014.
C#:
var lines = "1-6/2011\r\n02-12/2014\r\n12-30/2014\r\n12-31/2014\r\n12-32/2014\r\n13-31/2014\r\n20-6/2014";
var finds = Regex.Matches(lines, #"^(?:0?[1-9]|1[0-2])\s*[-−—]\s*(?:0?[1-9]|1[0-2])\s*/\s*\s*2[01][0-9]{2}\r?$", RegexOptions.Multiline);
Mind that \r? is only necessary in case we test with Multiline mode on. You can remove it when checking separate values.
So i have to select digits between 1-12
For that you can use regex
(?:0?[1-9]|1[0-2])
See demo.
https://www.regex101.com/r/fJ6cR4/23
You can use this regex:
^(?:[1-9]|1[0-2])\s*-\s*(?:[1-9]|1[0-2])\s*/\s*2[01]\d{2}$
RegEx Demo
Simplest regex to match with 1-12 is (1[0-2]?)|[2-9].
It matches with 13 cause 1[0-2]? matches with 1, but it doesn't matter in full regex (1[0-2]?)|[2-9]\/\d\d\d\d.

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.

.NET Regex: negate previous character for the first character in string

Consider following string
"Some" string with "quotes" and \"pre-slashed\" quotes
Using regex, I want to find all the double quotes with no slash before them. So I want the regex to find four matches for the example sentence
This....
[^\\]"
...would find only three of them. I suppose that's because of the regex's state machine which is first validating the command to negate the presence of the slash.
That means I need to write a regex with some kind of look-behind, but I don't know how to work with these lookaheads and lookbehinds...im not even sure that's what I'm looking for.
The following attempt returns 6, not 4 matches...
"(?<!\\)
"(?<!\\")
Is what you're looking for
If you want to match "Some" and "quotes", then
(?<!\\")(?!\\")"[a-zA-Z0-9]*"
will do
Explanation:
(?<!\\") - Negative lookbehind. Specifies a group that can not match before your main expression
(?!\\") - Negative lookahead. Specifies a group that can not match after your main expression
"[a-zA-Z0-9]*" - String to match between regular quotes
Which means - match anything that doesn't come with \" before and \" after, but is contained inside double quotes
You almost got it, move the quote after the lookbehind, like:
(?<!\\)"
Also be ware of cases like
"escaped" backslash \\"string\"
You can use an expression like this to handle those:
(?<!\\)(?:\\\\)*"
Try this
(?<!\\)(?<qs>"[^"]+")
Explanation
<!--
(?<!\\)(?<qs>"[^"]+")
Options: case insensitive
Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind) «(?<!\\)»
Match the character “\” literally «\\»
Match the regular expression below and capture its match into backreference with name “qs” «(?<qs>"[^"]+")»
Match the character “"” literally «"»
Match any character that is NOT a “"” «[^"]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “"” literally «"»
-->
code
try {
if (Regex.IsMatch(subjectString, #"(?<!\\)(?<qs>""[^""]+"")", RegexOptions.IgnoreCase)) {
// Successful match
} else {
// Match attempt failed
}
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}

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.

Categories