MatchCollection and Regex Matches weird behaviour - c#

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

Related

C# Regex is matching too much

My regex should work like this: https://regex101.com/r/dY9jI4/1
It should Match only Nicknames (personaname).
My C# code looks like this:
string pattern = #"personaname"":\s+""([^""]+)""";
string users = webClient.DownloadString("http://pastebin.com/raw/cDHTXXD3");
Regex regex = new Regex(pattern);
MatchCollection matches = regex.Matches(users);
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}
But in VS15 my regex in matching my whole pattern, so console output looks like:
personaname": "Tom"
personaname": "Emily"
Is something wrong with my pattern? How can I fix it?
So while the actual answer would be to just parse this JSON and get the data, your problem is in Match, you need match.Groups[1].Value to get that unnamed group.
string pattern = #"personaname"":\s+""(?<name>[^""]+)""";
string users = webClient.DownloadString("http://pastebin.com/raw/cDHTXXD3");
Regex regex = new Regex(pattern);
MatchCollection matches = regex.Matches(users);
foreach (Match match in matches)
{
Console.WriteLine(match.Groups["name"].Value);
}

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

Regex match between two strings that might contain another string

I'm doing a regex that is trying to match the following string:
.\SQL2012
From the two strings (they are contained within another larger string but that is irrelevant in this case):
/SERVER "\".\SQL2012\""
/SERVER .\SQL2012
So the "\" before and the \"" after the match may both be omitted in some cases. The regex I've come up with (from a previous question here on StackOverflow) is the following:
(?<=\/SERVER\s*(?:[""\\""]+)?)\w+(?=(?:[\\""""]+|$)| )
Which works fine if I'm trying to match TEST_SERVER instead of .\SQL2012 (because \w does not match special characters). Is there a way to match anything until \"" or a whitespace occurs?
I'm doing this in C#, here's my code:
string input = "/SERVER \"\\\".\\SQL2012\\\"\"";
string pattern = #"(?<=\/SERVER\s*(?:[""\\""]+)?)\w+(?=(?:[\\""""]+|$)| )";
Regex regEx = new Regex(pattern);
MatchCollection matches = regEx.Matches(input);
foreach (Match match in matches)
{
Console.WriteLine(match.ToString());
}
Console.ReadKey();
Add a word boundary \b just before to the lookahead,
string input = "/SERVER .\\SQL2012";
Regex rgx = new Regex(#"(?<=\/SERVER\s+""\\"").*?\b(?=\\""""|$| )|(?<=\/SERVER\s+).*?\b(?= |$)");
foreach (Match m in rgx.Matches(input))
Console.WriteLine(m.Groups[0].Value);
Console.WriteLine(input);
IDEONE

Regex find first 20 characters after an email stored in variable

string subject = null;
string toFind = "["+emailFound+"]([.20])";
Match match = Regex.Match(messageText, toFind, RegexOptions.IgnoreCase);
if (match.Success)
{
subject = match.Value;
}
MessageBox.Show(subject);
This is code I have so far, I'm really new at regex and not quite sure how it works. How would I get the first 20 characters after "emailFound"
Thanks
No more errors, but now it's just not finding anything after the emailFound...
That's because you need to get the Group value after you check the match instance.
string toFind = Regex.Escape(emailFound) + "(?s)(.{20})";
Match match = Regex.Match(messageText, toFind);
if (match.Success) {
subject = match.Groups[1].Value; // Get the Group value
}
See live demo
You can try this:
string toFind = Regex.Escape(emailFound)+"(?s)(.{20})";
(?s) allows the dot to match newlines (you can remove it and add the RegexOptions.SingleLine in the Match method.)
.{20} twenty characters

Match with reges excluding string tags

I am trying to write a code in order to get the matches in a list but without the match tags, until now i have built the following code in WP7 application written on C#
public static MatchCollection MatchTags(string content, string string_start, string string_end)
{
MatchCollection matches = Regex.Matches(content, string_start + "(.*?)" + string_end, RegexOptions.IgnoreCase);
return matches;
}
So how to return matches without string_start, string_end (match tags) without use of replace function after the match extraction?
Use lookarounds..
String.Format("(?<={0}).*?(?={1})",string_start,string_end);
Though you can also use groups.i.e in your regex (.*?) would capture the content within Group 1.No need of lookarounds then..
MatchTags(content,start,end).Cast<Match>()
.Select(x=>x.Groups[1].Value);
It works when i get the result with the next code:
string my_string_no_tags = matches[number].Groups[1].Value;
Consider the following code...
MatchCollection matches = Regex.Matches(content, string.Format("(?<={0}).*?(?={1})", string_start, string_end), RegexOptions.IgnoreCase);
return matches;
Good Luck!

Categories