Reputation: 14847
I created this Regex:
#define[\s]{1,}(?<name>[a-zA-Z0-9_$]){1,}[\s]{1,}(?<value>[a-zA-Z0-9_$\s()={},;.?:]){1,}
But when i test it this give me this result:
name: E
value: 6
String:
#define MAX_ARENE 10123456
How can i get all string in the group and not just the last char?
Anyway, if you need this is my C# code:
Match match = Regex.Match(line, @"#define[\s]{1,}([a-zA-Z0-9_$]){1,}[\s]{1,}([a-zA-Z0-9_$\s()={},;.]){1,}", RegexOptions.Singleline);
if (match.Success)
{
MessageBox.Show(match.Groups[1].ToString() + match.Groups[2].ToString());
defines.Add(match.Groups[1].ToString(), match.Groups[2].ToString());
node.Nodes.Add(match.Groups[1].ToString());
}
P.S Debug code.
I used this online tool http://regexhero.net/tester/ to test the Regex, but the same result happens in the MessageBox
Upvotes: 2
Views: 142
Reputation: 336108
When you're doing
(?<name>[a-zA-Z0-9_$]){1,}
you're repeating the capturing group itself (which matches a single character), thus overwriting each one-character match result with the next repetition. You want to use
(?<name>[a-zA-Z0-9_$]+)
Don't forget to do the same with value
; also, +
means "one or more", so use that instead of {1,}
.
[\s]
is superfluous as well - \s
is enough.
Finally, I think it's more straightforward to use match.Groups[n].Value
instead of match.Groups[n].ToString()
.
Upvotes: 7