开发者

regex for unselecting inside selection

开发者 https://www.devze.com 2023-02-22 05:09 出处:网络
An example, I have a string that contains let\'s say: function main { // TODOprint \'hello {cmd get world} world {nice}!\'s asdads

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'

开发者_如何学Python}

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消