Link to home
Start Free TrialLog in
Avatar of Tolgar
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:
-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,
Avatar of mvidas
mvidas
Flag of United States of America image

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
Avatar of Tolgar
Tolgar

ASKER

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,
Try

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

Open in new window

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.
Avatar of Tolgar

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,
Avatar of Tolgar

ASKER

@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

Avatar of Tolgar

ASKER

I think this will work:

content = content.replaceAll("^(?:.*?)(keyword.*?)(?: -r)?$", "$2");
Avatar of Tolgar

ASKER

No this didn't work. How should I do it?
Avatar of Tolgar

ASKER

@CEHJ: Yours work but how can I make it similar to mvidas approach so that it will use the "keyword" marker?

Thanks,
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland 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
ASKER CERTIFIED SOLUTION
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

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.
@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

Avatar of Tolgar

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
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
Avatar of Tolgar

ASKER

Sorry, this was what I was expecting.
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