Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

How to remove part of a string from another string by keeping some of it conditionally in Java?

I have string and I modify this string using the code below:

The input is:

-f S:\some\keyword\path\to\a\file.c
-d C:some\other\path\to\keyword\another\file.cpp

Open in new window


The code that I use is:

content = content.replaceAll("(?m)^.*?(?=keyword)", "")

Open in new window


The output is this:

keyword\path\to\a\file.c
keyword\another\file.cpp

Open in new window


Now, I would like to change this regexp in such a way that the output will be like this for the same input:

Expected output:

keyword\path\to\a\file.c
-d keyword\another\file.cpp

Open in new window


As you can see, the difference between the previous output and this one is the -d flag in the front. The idea is to keep the -d flag if there is any and do the rest same.

The input data is in one big string. I would prefer to do it in one regexp instead of looping through the lines.

How can I do?

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tolgar
Tolgar

ASKER

Great!