1

I have below text

    ABCDEF
     JHJHJNJN<098978686
     <jjg>
    HGHJFGV XXXX    
       10-10-2018
    JHKGHKGHG
        JKHJHHJM
10-10-2019 JGHHGHGVH
HBVJHBHBB

I want to replace date followed by XXX+number of spaces Here is my code

public class Regexreplace {

    public static void main(String[] args){

        String inp = "    ABCDEF"
                     +" JHJHJNJN<098978686"
                     +"     <jjg>         "
                     +" HGHJFGV XXXX     "
                     +"     10-10-2018  "
                     +" JHKGHKGHG  "
                     +"     JKHJHHJM"
                     +"     10-10-2019 JGHHGHGVH"
                     +" HBVJHBHB    ";
        String ipRegex = "(XXXX.*)[\\s]+([\\d]{1,2}-[\\d]{1,2}-[\\d]{4})";

        System.out.println(inp.replaceAll(ipRegex, "$110-11-2018"));

    }

}

Output:

ABCDEF  JHJHJNJN<098978686      <jjg>           HGHJFGV XXXX            10-10-2018      JHKGHKGHG       JKHJHHJM    10-11-2018 JGHHGHGVH    HBVJHBHB

Why its replacing the second date in text ?

Andrea
  • 109
  • 2
  • 10

3 Answers3

1

It's replacing the second date because you have a greedy "match anything" sequence in your regular expression:

"(XXXX.*)[\\s]+([\\d]{1,2}-[\\d]{1,2}-[\\d]{4})"
//    ^^
//   Here

Answers to this question explain the difference between greedy and non-greedy (lazy) quantifiers. In a nutshell:

The standard quantifiers in regular expressions are greedy, meaning they match as much as they can, only giving back as necessary to match the remainder of the regex.

For your input, this means that the .* sequence will match everything up to the line that contains the second date.

Since you don't have a need to "match anything", one solution would be to remove the greedy sequence altogether, and bring the whitespace matching sequence inside your capturing group:

"(XXXX\\s+)(\\d{1,2}-\\d{1,2}-\\d{4})"

Note: there's no need to put \\d or \\s inside a character class ([\\s]).

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
0

Try with this reg expression

"XXXX[\\s]+([\\d]{1,2}-[\\d]{1,2}-[\\d]{4})"
0

((XXXX\s*)(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)\d\d$)this will match all dd-mm-yyyy format date followed by XXXX.....