Search code examples
javaregexstringregexp-replace

replaceFirst in a regex with $1 - JAVA


I need to extract the following pattern from a string and return one of the possible matches according to the input.

I used regular expressions, through different possibilities and I have not managed to get the expected result:

input a): 71346 G249 USD 70045620 27/08/2020 001 / 004

input b): 71346 G249 USD 70045620/2020 27/08/2020 001 / 004

Try one

String result = data.replaceFirst ( "(.*?([0-9]{6,}\\/[0-9]{4}).*)|(.*?([0-9]{6,}).*)", "$1" );

Try two

String result = data.replaceFirst ( ".*?([0-9]{6,}\\/[0-9]{4})|([0-9]{6,}).*", "$1" );

Try three

String result = data.replaceFirst ( ".*?([0-9]{6,})([0-9]{6,}\\/[0-9]{4}).*", "$1" );

Expected result according to input:

input a): 70045620

input b): 70045620/2020


Solution

  • Using an alternation with capturing groups in that way will give you different groupnumbers depending on the data. If you want a single group in the replacement, you can make the second part optional.

    String[] strings = { 
        "71346 G249 USD 70045620 27/08/2020 001 / 004",
        "71346 G249 USD 70045620/2020 27/08/2020 001 / 004"
    };
    String regex = "^.*?\\b([0-9]{6,}(?:/[0-9]{4})?)\\b.*$";
    
    for (String s : strings) {
        System.out.println(s.replaceFirst(regex, "$1"));
    }
    

    See a Java demo

    Output

    70045620
    70045620/2020
    

    You could also find the match instead of using replaceFirst.

    \b[0-9]{6,}(?:/[0-9]{4})?
    

    For example

    String[] strings = { 
        "71346 G249 USD 70045620 27/08/2020 001 / 004",
        "71346 G249 USD 70045620/2020 27/08/2020 001 / 004"
    };
    String regex = "\\b[0-9]{6,}(?:/[0-9]{4})?";    
    Pattern pattern = Pattern.compile(regex);    
    
    for (String s : strings) {
        Matcher matcher = pattern.matcher(s);
        if (matcher.find()) {
            System.out.println(matcher.group(0));
        }
    }
    

    See another Java demo

    Output

    70045620
    70045620/2020