Regex to replace single backslashes, excluding those followed by certain chars

前端 未结 3 1404
闹比i
闹比i 2021-01-12 17:11

I have a regex expression which removes any backslashes from a string if not followed by one of these characters: \\ / or }.

It should turn this string:

<         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-12 17:43

    You need to watch out for an escaped backslash, followed by a single backslash. Or better: an uneven number of successive backslashes. In that case, you need to keep the even number of backslashes intact, and only replace the last one (if not followed by a / or {).

    You can do that with the following regex:

    (?

    and replace it with:

    $1
    

    where the first match group is the first even number of backslashes that were matched.

    A short explanation:

    (?

    In plain ENglish that would read:

    match an uneven number of back-slashes, (?:((\\\\)*)\\), not followed by \\ or { or /, (?![\\/{]), and not preceded by a backslash (?.

    A demo in Java (remember that the backslashes are double escaped!):

    String s = "baz\\\\\\foo\\bar\\\\batz\\/hi";
    System.out.println(s);
    System.out.println(s.replaceAll("(?

    which will print:

    baz\\\foo\bar\\batz\/hi
    baz\\foobar\\batz\/hi
    

    EDIT

    And a solution that does not need look-behinds would look like:

    ([^\\])((\\\\)*)\\(?![\\/{])
    

    and is replaced by:

    $1$2
    

    where $1 is the non-backslash char at the start, and $2 is the even (or zero) number of backslashes following that non-backslash char.

提交回复
热议问题