正则表达式替换单个反斜杠,排除后跟某些字符的那些 [英] Regex to replace single backslashes, excluding those followed by certain chars

查看:62
本文介绍了正则表达式替换单个反斜杠,排除后跟某些字符的那些的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正则表达式,如果后面没有跟以下字符之一:\/或},它会从字符串中删除任何反斜杠.

它应该把这个字符串:

foo\bar\\batz\/hi

进入这个:

foobar\\batz\/hi

但问题是它在处理每个反斜杠时都在处理.所以它遵循的规则是删除第一个反斜杠,并忽略第二个反斜杠,因为它后面跟着另一个反斜杠.但是当它到达第三个时,它会删除它,因为它后面没有另一个.

我当前的代码如下所示:str.replace(/\\(?!\\|\/|\})/g,"")

但结果字符串看起来像这样:foobar\batz\/hi

如何让它跳过第三个反斜杠?或者它是进行某种显式否定搜索的情况?替换类型的东西?例如.替换 '\',但不替换 '\\'、'\/' 或 '\}'?

请帮忙!:)

编辑

抱歉,我应该解释一下 - 我使用的是 javascript,所以我认为我不能做负面的回顾......

解决方案

您需要注意转义的反斜杠,后跟单个反斜杠.或者更好:连续反斜杠的数量奇数.在这种情况下,您需要保持偶数个反斜杠完整,并且只替换最后一个(如果后面没有 /{).

您可以使用以下正则表达式:

(?<!\\)(?:((\\\\)*)\\)(?![\\/{])

并将其替换为:

$1

其中第一个匹配组是匹配的第一个偶数反斜杠.

简短说明:

(?<!\\) # 往后看,不能有'\'(?:((\\\\)*)\\) # 匹配奇数个反斜杠并将偶数存储在第 1 组(?![\\/{]) # 展望未来,不能有 '\'、'/' 或 '{'

用简单的英语写成:

<块引用>

匹配奇数个反斜杠,(?:((\\\\)*)\\),后面没有\\{/(?![\\/{]) 前面没有反斜杠 (?.

Java 中的演示(记住反斜杠是双重转义的!):

String s = "baz\\\\\\foo\\bar\\\\batz\\/hi";System.out.println(s);System.out.println(s.replaceAll("(?<!\\\\)(?:((\\\\\\\\)*)\\\\)(?![\\\\/{])", "$1"));

将打印:

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

编辑

不需要后视的解决方案如下所示:

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

并被替换为:

$1$2

其中 $1 是开头的非反斜杠字符,$2 是该非反斜杠字符后面的偶数(或零)个反斜杠.>

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:

foo\bar\\batz\/hi

Into this:

foobar\\batz\/hi

But the problem is that it is dealing with each backslash as it goes along. So it follows the rule in that it removes that first backslash, and ignores the 2nd one because it is followed by another backslash. But when it gets to the 3rd one, it removes it, because it isn't followed by another.

My current code looks like this: str.replace(/\\(?!\\|\/|\})/g,"")

But the resulting string looks like this: foobar\batz\/hi

How do I get it to skip the 3rd backslash? Or is it a case of doing some sort of explicit negative search & replace type thing? Eg. replace '\', but don't replace '\\', '\/' or '\}'?

Please help! :)

EDIT

Sorry, I should have explained - I am using javascript, so I don't think I can do negative lookbehinds...

解决方案

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:

(?<!\\)          # looking behind, there can't be a '\'
(?:((\\\\)*)\\)  # match an uneven number of backslashes and store the even number in group 1
(?![\\/{])       # looking ahead, there can't be a '\', '/' or '{'

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("(?<!\\\\)(?:((\\\\\\\\)*)\\\\)(?![\\\\/{])", "$1"));

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.

这篇关于正则表达式替换单个反斜杠,排除后跟某些字符的那些的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆