regex for unselecting inside selection
An example,
I have a string that contains let's say:
function main {
// TODO print 'hello {cmd get world} world {nice}!'s asdads
hello 'l{o}l'.'asd'
How can I select only the words that are in within '''s and that are not inside a {}'s. This example would return the output:
match 1:
'hello {
} world {
}!'
match 2:
'l{
}l'
match 3:
'asd'
thanks a lot!
If you just wanted all six matching strings separately I would use ['}].*?['{]
but you seem to want three strings in which case I would first replace }[^']*?{
with }{
and then match on '.*?'
.
MatchCollection matches = Regex.Matches(myInput, "'[^']+'", RegexOptions.SingleLine | RegexOptions.MultiLine);
Now the trick is to only select the even indexes of the matches found.
This should get you what you're after, in two steps:
IEnumerable<string[]> captures =
// Get the 'single quoted' tokens
Regex.Matches(s, "'[^']*'").Cast<Match>()
// Split each token by { blocks }, but keep the curly braces.
.Select(quoteMatch => Regex.Split(quoteMatch.Value, @"(?<=\{)[^{}]*(?=\})"))
.ToArray();
The result is a collection of arrays of strings - each collection is a "match", and each string is a "group".
It is possible to do all of that in a single .Net regex, but it isn't pretty, and much harder to work with. Here's a working solution: http://ideone.com/qaceF , but I don't think it's a proper answer to the question when there are much simpler alternatives.
精彩评论