1

I have an app that uses Rx to receive data from a device on the serial port. So I have an IObservable<char> that I slice and dice into various strings. However, the device vendor added some debugging information that is enclosed in braces:

interesting stuff {debug stuff} interesting stuff

source ---a-b-c-{-d-e-b-u-g-}-d-e-f---|
          | | |               | | |
output ---a-b-c---------------d-e-f---|

I need to filter out (discard, ignore) the {debug stuff} from my character sequence?. Is there a simple way to do that? "When you see this character, ignore elements until you see this other character".

I looked at Until but that would terminate the sequence and I don't want that to happen...

4

1 回答 1

3

假设没有嵌套或不平衡的括号,这应该可以满足您的要求。

source
    .Scan((prev, c) =>
    {
        if (prev == '{')
            return c == '}' ? c : '{';
        else
            return c;
    })
    .Where(c => c != '{' && c != '}')

它将 in 之后的所有内容转换{{直到},然后过滤掉所有大括号。图表输出为:

source ---a-b-c-{-d-e-b-u-g-}-d-e-f---|
scan   ---a-b-c-{-{-{-{-{-{-}-d-e-f---|
          | | |               | | |
where  ---a-b-c---------------d-e-f---|
于 2015-08-10T03:38:34.327 回答