Regex and escaped and unescaped delimiter

前端 未结 5 1547
我寻月下人不归
我寻月下人不归 2020-12-18 00:38

question related to this

I have a string

a\\;b\\\\;c;d

which in Java looks like

String s = \"a\\\\;b\\\\\\\\;c;d\"         


        
5条回答
  •  -上瘾入骨i
    2020-12-18 01:22

    I do not trust to detect those cases with any kind of regular expression. I usually do a simple loop for such things, I'll sketch it using C since it's ages ago I last touched Java ;-)

    int i, len, state;
    char c;
    
    for (len=myString.size(), state=0, i=0; i < len; i++) {
        c=myString[i];
        if (state == 0) {
           if (c == '\\') {
                state++;
           } else if (c == ';') {
               printf("; at offset %d", i);
           }
        } else {
            state--;
        }
    }
    

    The advantages are:

    1. you can execute semantic actions on each step.
    2. it's quite easy to port it to another language.
    3. you don't need to include the complete regex library just for this simple task, which adds to portability.
    4. it should be a lot faster than the regular expression matcher.

提交回复
热议问题