转义序列无效 [英] Invalid escape sequence
本文介绍了转义序列无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想过滤掉所有包含1个数字和3个大写字母且总长度为4的单词。请参阅我的示例: http://gskinner.com/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: http://gskinner.com/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?
推荐答案
您的代码有两个问题:
-
您的模式错误,因为它允许更多数字 - 请参阅 http://regexr.com?32u3e
Java需要双重转义斜线......
Java requires double escape slashes...
使用正则表达式
\\b(?=[A-Z]*\\d[A-Z]*\\b)[A-Z\\d]{4}\\b
这篇关于转义序列无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文