转义序列无效

编程入门 行业动态 更新时间:2024-10-13 08:20:55
本文介绍了转义序列无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想过滤掉所有包含1个数字和3个大写字母且总长度为4的单词。请参阅我的示例: gskinner/RegExr/?32taa

I would like to filter out all words containing 1 number and 3 capital letters with a total length of 4. See my example here: gskinner/RegExr/?32taa

ArrayList<String> result = new ArrayList<String>(); for(int i = 0; i <= 10; i++){ String message = resp.data.get(i).message; Matcher m = MY_PATTERN.matcher("\b(?=[^\d]*\d[^\d]*)[A-Z\d]{4}\b"); while (m.find()) { String s = m.group(1); result.add(s); } }

但是当我将正则表达式模式传递给匹配器方法时,我收到错误:

But when i pass my regexp pattern to the matcher method, i get the error:

Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )

我是否需要交换 \d 还有另一封信吗?

Do I need to swap \d with another letter?

推荐答案

您的代码有两个问题:

  • 您的模式错误,因为它允许更多数字 - 请参阅 regexr?32u3e

Java需要双重转义斜线......

Java requires double escape slashes...

使用正则表达式

\\b(?=[A-Z]*\\d[A-Z]*\\b)[A-Z\\d]{4}\\b

更多推荐

转义序列无效

本文发布于:2023-10-08 15:02:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472973.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!