Java执行正则表达式

理查德

我正在使用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..

正则表达式测试器中,我的正则表达式是正确的,因此我可能在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";

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章