Tolgar
asked on
How to remove some string fom a larger string by using regexp in Java?
How can I remove some part of the following strings to get the expected output string:
Input:
Expected Output:
Input:
Expected Output:
The idea is remove everything that is before "keyword" and if there is any -r at the end remove that as well.
How can I write this regexp in Java?
Thanks,
Input:
-f someLetter:\Something1\keyword/some/path/to/a/file.c
Expected Output:
keyword/some/path/to/a/file.c
Input:
-d SomeLetter:\Something2\keyword/some/path/to/a/directory -r
Expected Output:
keyword/some/path/to/a/directory
The idea is remove everything that is before "keyword" and if there is any -r at the end remove that as well.
How can I write this regexp in Java?
Thanks,
ASKER
I am getting some sytax errors for this Java code.
I did the following and I am getting syntax errors in Eclipse for:
The other problem which is my mistake, I didn't mention that all these will be in one bitg String and each line is separated by new lines. I would like to make this replacement for each line all at once.
So the input is this:
And the output is this:
This is the code that I used from the code you sent to me:
Thanks,
I did the following and I am getting syntax errors in Eclipse for:
/[^\x00]*
and
^\
and
im
The other problem which is my mistake, I didn't mention that all these will be in one bitg String and each line is separated by new lines. I would like to make this replacement for each line all at once.
So the input is this:
-f someLetter:\Something1\keyword/some/path/to/a/file.c
-d SomeLetter:\Something2\keyword/some/path/to/a/directory -r
And the output is this:
keyword/some/path/to/a/file.c
keyword/some/path/to/a/directory
This is the code that I used from the code you sent to me:
var Match = content.match(/[^\x00]*?(keyword[^\x00]*?)( \-r)?$/im);
if (vMatch != null) {
content = vMatch[1];
}
//write the content to a file
rwFile.writeToFile(content, file);
Thanks,
Try
s = s.replaceAll(".*\\\\(.*)", "$1");
My apologies, you clearly said "java" and I gave you javascript. The "im" were for CASE_INSENSITIVE and MULTILINE which you'll want to use. Otherwise, try the following:
^(?:.*?)(keyword.*?)(?: -r)?$
I'm on my way out the door, but I'll check back in a bit.
ASKER
@CEHJ: I think you used the number of slashes to decide from which point to trim the string.
Can we make it more specific by using the "keyword"?
Because it will be guaranteed that there will be the marker "keyword" and I want everything that starts with this "keyword" marker.
Thanks,
Can we make it more specific by using the "keyword"?
Because it will be guaranteed that there will be the marker "keyword" and I want everything that starts with this "keyword" marker.
Thanks,
ASKER
@mvidas: This is what I was looking for. But how I am going to catch the mathes in here?
content.matches("^(?:.*?)(keyword.*?)(?: -r)?$");
ASKER
I think this will work:
content = content.replaceAll("^(?:.* ?)(keyword .*?)(?: -r)?$", "$2");
content = content.replaceAll("^(?:.*
ASKER
No this didn't work. How should I do it?
ASKER
@CEHJ: Yours work but how can I make it similar to mvidas approach so that it will use the "keyword" marker?
Thanks,
Thanks,
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@TerryAtOpus; what is multiline mode? Why do I need it?
With multiline mode on, ^ and $ will match the start and end of every line respectively, rather than just the start and end of the entire string.
If you don't use the ^ in your pattern, you'll run into trouble if the keyword appears more than once on the same line.
If you don't use the ^ in your pattern, you'll run into trouble if the keyword appears more than once on the same line.
@TerryAtOpus; what is multiline mode? Why do I need it?You would need it if you have a String that spans more than one line (i.e. it contains linefeeds). Is that the case?
Tolgar, i would advise you to test candidate answers before accepting them: in fact neither Terry's nor my answer works with both of your candidate strings. If you don't need multiline matching (no evidence so far that you do) then you might try instead:
s = s.replaceAll(".*?(keyword\\S*).*", "$1");
ASKER
both of the answers I accepted worked for me. But thanks for the additional info.
@Cehj: Btw, can you please take a look at this question? I was up all night and I could make the jnlp work.
https://www.experts-exchange.com/questions/28269372/How-to-create-jnlp-files-for-a-java-project.html?anchorAnswerId=39579330#a39579330
@Cehj: Btw, can you please take a look at this question? I was up all night and I could make the jnlp work.
https://www.experts-exchange.com/questions/28269372/How-to-create-jnlp-files-for-a-java-project.html?anchorAnswerId=39579330#a39579330
both of the answers I accepted worked for me.Then you didn't specify your requirement correctly:
Input:Both answers give the following as the output:
-d SomeLetter:\Something2\keyword/some/ path/to/a/ directory -r
Expected Output:
keyword/some/path/to/a/directory
keyword/some/path/to/a/dir
ASKER
Sorry, this was what I was expecting.
OK. So i hope, for multiline replacement of something like
you're expecting
as that's what you're going to get
"-d SomeLetter:\\Something2\\\r\nkeyword/some/keyword/path/to/a/directory -r";
you're expecting
-d SomeLetter:\Something2\
keyword/some/keyword/path/to/a/directory -r
as that's what you're going to get
Try this:
Open in new window
Tested using:Open in new window
Results:Open in new window
Matt