c# RegExp shows only one capture - c#

private String pattern = #"^{(.*)|(.*)}$";
ret = groups[0].Captures.Count.ToString(); // returns 1
isn't it should return 2 captures? because I have two () in my RegExp?
my string for example:
{test1 | test2}
the first capture should be test1 and the secnd test2, but I get the whole string in return and the captures count is 1 why is that?
UPDATE:
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(_sourceString);
String ret = "";
foreach (Match match in matches)
{
GroupCollection groups = match.Groups;
ret = groups[0].Captures[0].Value;
}
return ret; //returns the whole string, but I've expected 'test1'

| has a special meaning in regular expression. A|B matches A or B.
To match | literally, you need to escape it:
#"^{(.*)\|(.*)}$";

Related

Regex - Match multiple times in a string

I am trying to do a regex search on 'NNTSY` so that I can get two matches.
NNTS
NTSY
When I attempted to match using the pattern ?<NGrlyosylation>N[^P][ST][^P])", I am only getting one match, which is NNTS.
How can I use Regex to match NNTSY so that two matches can be found?
NOTE: Background info: Rosalind problem can be found here.
Here is my code.
input = "NNTSY";
Regex regex = new Regex("(?<NGrlyosylation>N[^P][ST][^P])", RegexOptions.Compiled | RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(input);
foreach (Match match in matches)
{
// Need to add 1 to because match index is 0 based
const int offset = 1;
yield return match.Index + offset;
}
Finding overlapping matches is usually not allowed in most programming languages (except few).
So, I don't think there exists a pure regex way to solve this, but you can use Substring in C# with lookahead as
(?=N[^P][ST][^P]).
C# Code
string input = "NNTSY";
Regex regex = new Regex("(?=N[^P][ST][^P]).", RegexOptions.Compiled | RegexOptions.IgnoreCase);
Match match = regex.Match(input);
while (match.Success)
{
Console.WriteLine(input.Substring(match.Index, 4));
match = match.NextMatch();
}
Ideone Demo

MatchCollection and Regex Matches weird behaviour

When I try to use a Regex to split a string the MatchCollection returned by .Matches only contain the string it self and not the group.
So here is my regex : string pattern = #"^(\w[.])\s*(\w+)$";
and a sample string : W.Test
I expect the MatchCollection to have 2 elements W. and Test but it looks like it doesnt work.
You need to return the matched context from your capturing groups. The Groups property gets the captured groups within the regular expression.
string pattern = #"^(\w[.])\s*(\w+)$";
string input = "W.Test";
Match match = Regex.Match(input, pattern);
if (match.Success) {
Console.WriteLine(match.Groups[1].Value); //=> 'W.'
Console.WriteLine(match.Groups[2].Value); //=> 'Test'
}
Try this
Match match = Regex.Match(input, #" ^(?<firstGroup>\w[.])\s*(?<SecondGroup>\w+)$", RegexOptions.IgnoreCase);
var firstGroup = match.Groups["firstGroup"].Value

Regex for a special string pattern on multiple levels

I need to evaluate a string pattern that is something like as given below and I am quite new at writing such complex expressions
<%(ddlValue){DropDownList}[SelectedValue]%>
// this has three part (Control Name) {Control Type} [Control Property]
I tried a whole lot of regex and other tools like RegExr but anything did not worked. I have to do this on four levels, that is as given below in the code. So here is what I have done:
string regex = "/[<%](.*?)[%>]/g"; // Regex to match "<% %>" pattern
Match mtch = Regex.Match(strQuery, regex, RegexOptions.Singleline);
string strControlName = "";
string strControlType = "";
string strControlProp = "";
if (mtch.Success)
{
string strVal = mtch.Value;
Match mtchControlName = Regex.Match(strVal, "/[(]\S)/");
// Regex to match "()" i.e. control name ("ddlValue" in above example)
if (mtchControlName.Success)//Match control Name
{
strControlName = mtchControlName.Value;
Match mtchControlType = Regex.Match(strVal, "/[{]\s[}]/");
// Regex to match "[]" i.e. control type
if (mtchControlType.Success) // Match Control Type
{
strControlType = mtchControlType.Value;
Match mtchControlProp = Regex.Match(strVal, "/[(]\S[)]/");
// Regex to match "[]" i.e. control property
if (mtchControlProp.Success) // Match Control Prop
{
strControlProp = mtchControlProp.Value;
}
}
}
}
You can do this in a single regex. Being as specific as possible, you could do this:
Regex regexObj = new Regex(
#"\( # Match (
( # Capture in group 1:
[^()]* # Any number of characters except ()s
) # End of group 1
\) # Match )
\{([^{}]*)\} # The same for {...} into group 2
\[([^\[\]]*)\] # The same for [...] into group 3",
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
Then you case use
Match matchResults = regexObj.Match(subjectString);
to get a Match object. Access the submatches via
matchResults.Groups(n).Value // insert 1, 2 or 3 for n
See it live on regex101.com.
You can use capturing groups in one expression to capture all groups together:
String input = "<%(ddlValue){DropDownList}[SelectedValue]%>";
String pattern = #"<%\((.+)\)\{(.+)\}\[(.+)\]%>";
Match m = Regex.Match(input, pattern);
if (m.Groups.Count == 4)
{
string firstpart = m.Groups[1].ToString();
string secondpart = m.Groups[2].ToString();
string thirdpart = m.Groups[3].ToString();
}
You could use named groups. It makes the code more readable.
Match m = Regex.Match(inputData, #"^<%\((?<ddlValue>[^)]+)\){(?<DropDownList>[^}]+)}\[(?<SelectedValue>[^\]]+)\]%>$");
if (m.Groups.Count == 4)
{
string firstpart = m.Groups["ddlValue"].ToString();
string secondpart = m.Groups["DropDownList"].ToString();
string thirdpart = m.Groups["SelectedValue"].ToString();
}

Regex with C#: How do I replace a string that matches a specific group

Given a regex with 2 groups:
Regex regex = new Regex("^[^{}]*(\\{([^}]*)\\})[^{}]*$"); // group 1 matches {exp} (with braces)
// group 2 matches exp (without braces)
How do I replace a match in the first group?
string inputStr = "mystring{valueToRaplace}"
string s = regex .Replace(inputStr, m => ???);
For instance I want to specify whether to match and replace {valueToRaplace} (group 1) or valueToRaplace (group 2).
I think this is what you want
Regex regex = new Regex("^[^{}]*(\\{([^}]*)\\})[^{}]*$");
string inputStr = "mystring{valueToRaplace}";
string replaceWithThis = "Replace with this";
//change m.Groups[2] to whichever index you want to replace
string final = regex.Replace(inputStr,
new MatchEvaluator(new Func<Match, string>(m => inputStr.Replace(m.Groups[2].Value, replaceWithThis))));

C# Regular Expressions, string between single quotes

string val = "name='40474740-1e40-47ce-aeba-ebd1eb1630c0'";
i want to get the text between ' quotes using Regular Expressions.
Can anyone?
Something like this should do it:
string val = "name='40474740-1e40-47ce-aeba-ebd1eb1630c0'";
Match match = Regex.Match(val, #"'([^']*)");
if (match.Success)
{
string yourValue = match.Groups[1].Value;
Console.WriteLine(yourValue);
}
Explanation of the expression '([^']*):
' -> find a single quotation mark
( -> start a matching group
[^'] -> match any character that is not a single quotation mark
* -> ...zero or more times
) -> end the matching group
You are looking to match GUID's in a string using a regular expression.
This is what you want, I suspect!
public static Regex regex = new Regex(
"(\\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-"+
"([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\\}{0,1})",RegexOptions.CultureInvariant|RegexOptions.Compiled);
Match m = regex.Match(lineData);
if (m.Succes)
{
...
}
This will extract the text between the first and last single quote on a line:
string input = "name='40474740-1e40-47ce-aeba-ebd1eb1630c0'";
Regex regName = new Regex("'(.*)'");
Match match = regName.Match(input);
if (match.Success)
{
string result = match.Groups[1].Value;
//do something with the result
}
You could use positive lookahead and lookbehind also,
string val = "name='40474740-1e40-47ce-aeba-ebd1eb1630c0'";
Match match = Regex.Match(val, #"(?<=')[^']*(?=')");
if (match.Success)
{
string yourValue = match.Groups[0].Value;
Console.WriteLine(yourValue);
}

Categories