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:
-f someLetter:\Something1\keyword/some/path/to/a/file.c

Open in new window


Expected Output:
keyword/some/path/to/a/file.c

Open in new window


Input:
-d SomeLetter:\Something2\keyword/some/path/to/a/directory -r

Open in new window


Expected Output:
keyword/some/path/to/a/directory

Open in new window


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,
TolgarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mvidasCommented:
Hi Tolgar,

Try this:
function fTolgar(vInput) {
 var vMatch=vInput.match(/[^\x00]*?(keyword[^\x00]*?)( \-r)?$/im);
 if (vMatch != null) {
  return vMatch[1];
 } else {
  return "no match";
 }
}

Open in new window

Tested using:
document.write(fTolgar('-f someLetter:\Something1\keyword/some/path/to/a/file.c'));
document.write('<br />');
document.write(fTolgar('-d SomeLetter:\Something2\keyword/some/path/to/a/directory -r'));

Open in new window

Results:
keyword/some/path/to/a/file.c
keyword/some/path/to/a/directory

Open in new window

Matt
0
TolgarAuthor Commented:
I am getting some sytax errors for this Java code.

I did the following and I am getting syntax errors in Eclipse for:

/[^\x00]*

Open in new window

and
^\

Open in new window

and
im

Open in new window


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

Open in new window


And the output is this:
keyword/some/path/to/a/file.c
keyword/some/path/to/a/directory

Open in new window


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);

Open in new window


Thanks,
0
CEHJCommented:
Try

s = s.replaceAll(".*\\\\(.*)", "$1");

Open in new window

0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

mvidasCommented:
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)?$

Open in new window

I'm on my way out the door, but I'll check back in a bit.
0
TolgarAuthor Commented:
@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,
0
TolgarAuthor Commented:
@mvidas: This is what I was looking for. But how I am going to catch the mathes in here?

         
content.matches("^(?:.*?)(keyword.*?)(?: -r)?$");

Open in new window

0
TolgarAuthor Commented:
I think this will work:

content = content.replaceAll("^(?:.*?)(keyword.*?)(?: -r)?$", "$2");
0
TolgarAuthor Commented:
No this didn't work. How should I do it?
0
TolgarAuthor Commented:
@CEHJ: Yours work but how can I make it similar to mvidas approach so that it will use the "keyword" marker?

Thanks,
0
CEHJCommented:
s = s.replaceAll(".*?(keyword.*)", "$1");
                                            

Open in new window

0
Terry WoodsIT GuruCommented:
An alternative pattern (CEHJ's modified) is:

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

Open in new window

The (?m) activates multiline mode. If you accept my answer, please split points with CEHJ.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TolgarAuthor Commented:
@TerryAtOpus; what is multiline mode? Why do I need it?
0
Terry WoodsIT GuruCommented:
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.
0
CEHJCommented:
@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");

Open in new window

0
TolgarAuthor Commented:
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.

http://www.experts-exchange.com/Programming/Languages/Java/Q_28269372.html#a39579330
0
CEHJCommented:
both of the answers I accepted worked for me.
Then you didn't specify your requirement correctly:
Input:

-d SomeLetter:\Something2\keyword/some/path/to/a/directory -r

Expected Output:

keyword/some/path/to/a/directory
Both answers give the following as the output:

keyword/some/path/to/a/directory -r
0
TolgarAuthor Commented:
Sorry, this was what I was expecting.
0
CEHJCommented:
OK. So i hope, for multiline replacement of something like

"-d SomeLetter:\\Something2\\\r\nkeyword/some/keyword/path/to/a/directory -r";

Open in new window


you're expecting

-d SomeLetter:\Something2\
keyword/some/keyword/path/to/a/directory -r

Open in new window


as that's what you're going to get
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.