Java 执行正则表达式

标签 java regex

我使用的是 Java 8,并且有以下正则表达式:

https://regex101.com/r/XEgPWe/1

如您所见,它识别数字。

我正在尝试实现一个 Java 类,该类用“X”字符替换正则表达式匹配的值。

package com.jobs.spring.service;

public class ReplaceServiceImpl implements ReplaceService {

    private static final String REGEX_NUMBERS = "/\b(?:zero|nil|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixfteen|seventeen|eighteen|nineteen|een|twee|drie|vier|fyf|ses|sewe|agt|nege|tien|iqanda|Kunye|Kubili|Kuthathu|Kune|Kuhlanu|Yisithupa|Yisikhombisa|Yisishiyagalombili|Yisishiyagalolunye|nnoto|nngwe|pedi|tharo|nne|hlano|tshelela|supa|robedi|robong|leshome|unothi|inye|zimbini|zintathu|zine|zintlanu|zintandathu|isixhenxe|sisibhozo|lithoba|cero|uno|dos|tres|cuatro|cinco|seis|siete|ocho|nueve|diez|Zéro|Un|Deux|Trois|Quatre|Cinq|Sept|Huit|Neuf|Dix|eins|zwei|drei|fünf|sechs|sieben|acht|neun|zehn|elf|[0-9])(?:.{0,10}(?:zero|nil|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|een|twee|drie|vier|fyf|ses|sewe|agt|nege|tien|iqanda|Kunye|Kubili|Kuthathu|Kune|Kuhlanu|Yisithupa|Yisikhombisa|Yisishiyagalombili|Yisishiyagalolunye|nnoto|nngwe|pedi|tharo|nne|hlano|tshelela|supa|robedi|robong|leshome|unothi|inye|zimbini|zintathu|zine|zintlanu|zintandathu|isixhenxe|sisibhozo|lithoba|cero|uno|dos|tres|cuatro|cinco|seis|siete|ocho|nueve|diez|Zéro|Un|Deux|Trois|Quatre|Cinq|Sept|Huit|Neuf|Dix|eins|zwei|drei|fünf|sechs|sieben|acht|neun|zehn|elf|[0-9])){4,}\b/gi";

    @Override
    public String removePII(String input) {
        input = input.replaceAll(REGEX_NUMBERS, "X");
        return input;
    }

    public static void main(String[] args) {
        ReplaceService rep = new ReplaceServiceImpl();
        System.out.println(rep.removePII("hello some text 1234567890 more.."));
    }

}

我希望输出为:

hello some text XXXXXXXXXX more..

但它是:

hello some text 1234567890 more..

regex tester 来看,我的正则表达式是正确的,所以我可能在我的 Java 中做错了什么。

欢迎任何建议。

谢谢。

最佳答案

您需要解决几个问题:

  • 删除正则表达式分隔符(/.../ 末尾带有修饰符)
  • /i 替换为内联修饰符版本 (?i)(或将 Pattern.CASE_INSENSITIVE 选项传递给 Matcher 实例)
  • 加倍反斜杠(因为正则表达式转义是由文字 \ 构成的)

使用

private static final String REGEX_NUMBERS = "(?i)\\b(?:zero|nil|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixfteen|seventeen|eighteen|nineteen|een|twee|drie|vier|fyf|ses|sewe|agt|nege|tien|iqanda|Kunye|Kubili|Kuthathu|Kune|Kuhlanu|Yisithupa|Yisikhombisa|Yisishiyagalombili|Yisishiyagalolunye|nnoto|nngwe|pedi|tharo|nne|hlano|tshelela|supa|robedi|robong|leshome|unothi|inye|zimbini|zintathu|zine|zintlanu|zintandathu|isixhenxe|sisibhozo|lithoba|cero|uno|dos|tres|cuatro|cinco|seis|siete|ocho|nueve|diez|Zéro|Un|Deux|Trois|Quatre|Cinq|Sept|Huit|Neuf|Dix|eins|zwei|drei|fünf|sechs|sieben|acht|neun|zehn|elf|[0-9])(?:.{0,10}(?:zero|nil|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|een|twee|drie|vier|fyf|ses|sewe|agt|nege|tien|iqanda|Kunye|Kubili|Kuthathu|Kune|Kuhlanu|Yisithupa|Yisikhombisa|Yisishiyagalombili|Yisishiyagalolunye|nnoto|nngwe|pedi|tharo|nne|hlano|tshelela|supa|robedi|robong|leshome|unothi|inye|zimbini|zintathu|zine|zintlanu|zintandathu|isixhenxe|sisibhozo|lithoba|cero|uno|dos|tres|cuatro|cinco|seis|siete|ocho|nueve|diez|Zéro|Un|Deux|Trois|Quatre|Cinq|Sept|Huit|Neuf|Dix|eins|zwei|drei|fünf|sechs|sieben|acht|neun|zehn|elf|[0-9])){4,}\\b";

关于Java 执行正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44042310/

相关文章:

java - 为什么 replaceAll 以 "illegal group reference"失败?

java - JPanel 在 JScrollPane 中填充了按钮

Java编译不产生.jar

javascript - Unicode 正则表达式 : Compilation failed: range out of order in character class

java - 正则表达式如何将字符串的一部分替换为另一个字符串

c++ - 这个boost c++正则表达式代码有什么问题?

java - Android:如何将区域设置设置为 zh_HK android

regex - 删除一行中的重复字符,包括空格

regex - 避免字母数字字符串上的前导和尾随空格

java - 与 char 编码集相关的 XML 解析错误