在Java中使用正则表达式拆分具有转义序列的字符串 [英] Splitting a string that has escape sequence using regular expression in Java

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

问题描述

要拆分的字符串

abc:def:ghi:klm:nop

字符串应该基于:"分割"是转义字符.所以:"不应该被视为标记.

String should be split based on ":" "" is escape character. So ":" should not be treated as token.

split(":") 给出

split(":") gives

[abc]
[def]
[ghi]
[klm]
[nop]

所需的输出是字符串数组

Required output is array of string

[abc]
[def]
[ghi:klm]
[nop]

如何忽略:

推荐答案

使用 后视断言:

split("(?<!\\):")

这只会在没有前面的 时匹配.使用双重转义 \\ 是必需的,因为字符串声明需要一个,正则表达式需要一个.

This will only match if there is no preceding . Using double escaping \\ is required as one is required for the string declaration and one for the regular expression.

但是请注意,如果您想允许标记以反斜杠结尾,这将不允许您转义反斜杠.为此,您必须首先用

Note however that this will not allow you to escape backslashes, in the case that you want to allow a token to end with a backslash. To do that you will have to first replace all double backslashes with

string.replaceAll("\\\\", ESCAPE_BACKSLASH)

(其中 ESCAPE_BACKSLASH 是一个不会出现在您的输入中的字符串)然后,在使用后视断言进行拆分后,将 ESCAPE_BACKSLASH 字符串替换为未转义的反斜杠

(where ESCAPE_BACKSLASH is a string which will not occur in your input) and then, after splitting using the look-behind assertion, replace the ESCAPE_BACKSLASH string with an unescaped backslash with

token.replaceAll(ESCAPE_BACKSLASH, "\\")

这篇关于在Java中使用正则表达式拆分具有转义序列的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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