Java executes Regex

I am using Java 8 and have the following regex:

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

As you can see, it identifies numbers.

I am trying to implement a Java class that replaces regex values ​​with "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.."));
    }

}

      

I expect the output to be:

hello some text XXXXXXXXXX more..

      

but this:

hello some text 1234567890 more..

      

From regex , my regex is correct, so I am probably doing something wrong in my Java.

Any guidance is appreciated.

Thank.

+3


source to share


2 answers


There are several problems you need to fix:

  • remove regex separators ( /.../

    with modifiers at the end)
  • replace /i

    with inline modifier version (?i)

    (or pass parameter to Pattern.CASE_INSENSITIVE

    instance Matcher

    )
  • double backslash (as regex alignment is formed with literal \

    )


Using

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";

      

+3


source


I think you are using the wrong output in Java. Your regex should be:



String regex = "\\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";

      

+1


source







All Articles