正则表达式仅在字符串未转义时匹配字符串中的字符 [英] RegEx for matching a character in a string only if it isn't escaped

查看:30
本文介绍了正则表达式仅在字符串未转义时匹配字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果字符串中的字符没有被转义(即以另一个字符的奇数开头),我如何才能找到并对其进行操作?

How can I find and operate on a character in a string only if it isn't escaped (ie proceeded by an odd number of another character)?

示例:

所需字符:|

转义符:\

| 应该被找到(并对其进行操作,例如 split)

| should be found (and operated on, for example split)

\| 应该

\\| 应该

\\\| 应该

推荐答案

使用否定后视来定义边界:

Use a negative a lookbehind to define a boundary:

(?<!\\)(?:\\\\)*\|

查看现场演示

注意 Java 中的反斜杠,在正则表达式之上是:

Taking care of backslashes in Java, above regex would be:

(?<!\\\\)(?:\\\\\\\\)*\\|

这篇关于正则表达式仅在字符串未转义时匹配字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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