我正在使用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] 删除。
我来说两句