Oracle Nerd
Oracle Nerd

Reputation: 154

Write a Regex for a string

Hi I have a file with a lot of bad data lines. I've identified the lines with bad data. The file is very big that it cant be done manually. The problem may reoccur in future so I'm writing a small tool in java to remove the bad segments based on a input regex and remove it.

An example of Bad data is

ABC*HIK*UG*XY\17

I'm trying to write a regex for the above string. So far

Only "(^ABC)" works and ABC is removed.

When I use this nothing happens.

"(^ABC*.XY\17$)"

Please give your inputs.

EDITED:

The answer is working perfect but

If my input files contains this

ABC
123
ABC*HIK*UG*XY\17
1025
KHJ*YU*789

I should get output like

ABC
123
1025
KHJ*YU*789

but I'm getting like this

ABC
123

1025
KHJ*YU*789

Upvotes: 0

Views: 74

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

Change your pattern to,

"^ABC.*XY\\\\17$"

In java, you need to escape the backslash three more times in-order to match a single \ character. And the pattern to match any character zero or more times must be like .* not *. And also you don't need to put your pattern inside a capturing group.

String s = "ABC\n" + 
        "123\n" + 
        "ABC*HIK*UG*XY\\17\n" + 
        "1025\n" + 
        "KHJ*YU*789";
System.out.println(s.replaceAll("(?m)^ABC.*XY\\\\17\n?", ""));

Output:

ABC
123
1025
KHJ*YU*789

Since we are using anchors in our regex, we need to add the modifier. In our case, we need to add multi-line modifier (?m)

Upvotes: 1

Related Questions