简体   繁体   English

java.util.regex.PatternSyntaxException:索引附近非法重复

[英]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+")";

See the Java demo . 请参阅Java演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.util.regex.PatternSyntaxException:索引 12 附近的非法重复 - java.util.regex.PatternSyntaxException: Illegal repetition near index 12 java.util.regex.PatternSyntaxException:使用ant在索引0附近非法重复 - java.util.regex.PatternSyntaxException: Illegal repetition near index 0 using ant 引起原因:java.util.regex.PatternSyntaxException:非法重复 - Caused by: java.util.regex.PatternSyntaxException: Illegal repetition 线程“main”中的异常 java.util.regex.PatternSyntaxException:索引 9 附近的非法字符范围 - Exception in thread “main” java.util.regex.PatternSyntaxException: Illegal character range near index 9 线程“主”java.util.regex.PatternSyntaxException 中的异常:索引 2 附近的转义序列非法/不受支持 - Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 2 java.util.regex.PatternSyntaxException:索引0附近的未闭合字符类 - java.util.regex.PatternSyntaxException: Unclosed character class near index 0 java.util.regex.PatternSyntaxException:在索引 0 + 附近悬空元字符 &#39;+&#39; - java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0 + java.util.regex.PatternSyntaxException:索引附近的未闭合字符类 - java.util.regex.PatternSyntaxException: Unclosed character class near index 获取java.util.regex.PatternSyntaxException:尝试用replaceAll()替换String时,非法重复 - Getting java.util.regex.PatternSyntaxException: Illegal repetition when trying to replace String with replaceAll() java.util.regex.PatternSyntaxException:索引附近的正则表达式模式中的语法错误 - java.util.regex.PatternSyntaxException: Syntax error in regex pattern near index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM