I am looking for a Regex to read a c# class file like
MyClass{}
The regex shld return "MyClass" when passed the string "MyClass{}".
Edited:
Common
{
MyClass1
{
Method1
{
开发者_如何学Python "Helloworld";
"GoodBye";
}
Method2
{
"SayGoodMorning";
}
}
MyClass2
{
Method3
{
"M3";
}
}
}
Actually i have to read a hierarchy like above, there can b n number of it and have to read before and inside {}
If your regex engine supports lookahead, then you can use
\b[^{\s]+(?=\s*\{)
This will match ["Common", "MyClass1", "Method1", "Method2", "MyClass2", "Method3"]
in your example.
Explanation:
\b
: Start the match at a word boundary.
[^{\s]+
: Match one or more characters except opening braces or whitespace.
(?=\s*\{)
: Assert that the match ends with a character that is followed by optional whitespace (including linebreaks) and an opening brace.
Restricting matches to certain hierarchy levels (in this case, only match at the second level of nesting) is not possible with regular expressions in general. It may be possible in certain regex dialects, but this is stretching the limits of what regexes are designed for - a parser would suit this better.
精彩评论