I am having trouble capturing parentheses.
I have a big file with data of this form:
I.u[12] = {n: "name1",...};
I.u[123] = {n: "name2",...};
I.u[1234] = {n: "name3",...};
I want to create a system which helps me get the name (here name1, name2, name3) out of the file if I provide the id (here 12, 123, 1234). I have the following code:
public static string GetItemName(int id)
{
Regex regex = new Regex(@"^I.u\["+id+@"\]\s=\s{n:\s(.+),.+};$");
Match m= GetMatch(regex,filepath);
if(m.Success) return m.Groups[0].Value;
else return "unavailable";
}
public static Match GetMatch(Regex regex, string filePath)
{
Match res = null;
using (StreamReader r = new StreamReader(filePath))
{
string line;
while ((line = r.ReadLine()) != null)
{
res = regex.Match(line);
if (res.Success) break;
}
}
return res;
}
The regex finds the correct line in the file but I really don’t know why it doesn’t extract the name as I wanted and,
if(m.Success) return m.Groups[0].Value;
returns me the whole line in the file and not the name… I tried a lot of things, even changing m.Groups[0] to m.Groups[1] but it didn’t work.
I have searched for a moment now without success. Would you have an idea of what is wrong?
Based on your updated question, I can see that you are using a greed quantifier:
.+. This will match as much as possible. You want a passive modifier, which will only match as much as necessary:.+?Try this:
Then: