[英]java.util.regex.PatternSyntaxException: Illegal repetition near index
I am using Java8 and would like to use the following Regex to filter an address from a string, but I get this error: 我正在使用Java8,并且想使用以下正则表达式从字符串中过滤地址,但出现此错误:
Error 错误
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 18
(\d+\s*(\w+ ){1,2}${(?i)\b(street|st|road|rd|avenue|ave|drive|dr|loop|court|ct|circle|cir|lane|ln|boulevard|blvd|way)\.?\b}(\s+${(?i)\b(apt|bldg|dept|fl|hngr|lot|pier|rm|ste|slip|trlr|unit|#)\.? *[a-z0-9-]+\b})?)|(${/P\.? ?O\.? *Box +\d+})
^
Code 码
private static final String REGEX_ROAD = "(?i)\\b(street|st|road|rd|avenue|ave|drive|dr|loop|court|ct|circle|cir|lane|ln|boulevard|blvd|way)\\.?\\b";
private static final String REGEX_APT = "(?i)\\b(apt|bldg|dept|fl|hngr|lot|pier|rm|ste|slip|trlr|unit|#)\\.? *[a-z0-9-]+\\b";
private static final String REGEX_POBOX = "/P\\.? ?O\\.? *Box +\\d+";
private static final String REGEX_STREET = "(\\d+\\s*(\\w+ ){1,2}${"+REGEX_ROAD+"}(\\s+${"+REGEX_APT+"})?)|(${"+REGEX_POBOX+"})";
input = input.replaceAll(REGEX_STREET, "<ADDRESS>");
Any help appreciated. 任何帮助表示赞赏。
Entire Class: 整个班级:
package com.jobs.spring.service.replace;
public class ReplaceServiceImpl implements ReplaceService {
private static final String REGEX_ROAD = "(?i)\\b(street|st|road|rd|avenue|ave|drive|dr|loop|court|ct|circle|cir|lane|ln|boulevard|blvd|way)\\.?\\b";
private static final String REGEX_APT = "(?i)\\b(apt|bldg|dept|fl|hngr|lot|pier|rm|ste|slip|trlr|unit|#)\\.? *[a-z0-9-]+\\b";
private static final String REGEX_POBOX = "/P\\.? ?O\\.? *Box +\\d+";
private static final String REGEX_STREET = "(\\d+\\s*(\\w+ ){1,2}${"+REGEX_ROAD+"}(\\s+${"+REGEX_APT+"})?)|(${"+REGEX_POBOX+"})";
@Override
public String removePII(String input) {
input = input.replaceAll(REGEX_STREET, "<ADDRESS>");
return input;
}
public static void main(String[] args) {
ReplaceService rep = new ReplaceServiceImpl();
System.out.println(rep.removePII("1234 Flex Road and 21 happy street"));
}
}
Java does not support string interpolation, the ${...}
JavaScript-like template literal placeholders are not supported and are treated as literal symbols. Java不支持字符串插值,不支持
${...}
类似JavaScript的模板文字占位符,并将其视为文字符号。
Since $
means the end of a string and is a zero-width assertion, it should not be quantified. 由于
$
表示字符串的结尾并且是零宽度的断言,因此不应将其量化。 However, Java regex engine is lenient to the user and allows the usage of a quantifier with a zero-width assertion (you may use ${5}
although it makes no sense, there can only be a single end of a string at a given position). 但是,Java regex引擎对用户宽容,并允许使用带有零宽度断言的量词(您可以使用
${5}
尽管这没有任何意义,给定字符串只能有一个结尾位置)。
The major problem here is that {
(start of a limiting quantifier) must be followed with a number denoting the number of repetitions and if there is a symbol other than a digit, anything but a number followed with }
or ,MAX_REPEATITION_VALUE}
, the Illegal repetition error will show up. 此处的主要问题是
{
(一个限定词的开头)后面必须有一个数字,该数字表示重复的次数,并且如果除数字之外还有一个符号,则除数字之外的任何,MAX_REPEATITION_VALUE}
加上}
或,MAX_REPEATITION_VALUE}
,这是非法的将会显示重复错误。
So, just remove ${
and }
. 因此,只需删除
${
和}
。
String REGEX_STREET = "(\\d+\\s*(\\w+ ){1,2}"+REGEX_ROAD+"(\\s+"+REGEX_APT+")?)|("+REGEX_POBOX+")";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.