How to write a regexp that matches starting from a string to another string in Java?

Hi,
I have the following string and I would like to capture only the part of it.

The original string is

-f K:\something1\EXCHANGE\something2 \something3\something4\something.c

Open in new window


What I would like to capture is:

K:\something1\

Open in new window



The following regexp captures everything upto EXCHANGE. But I don't want to capture -f in the beginning. (-f can also be -d )

^.+?(?=(?:EXCHANGE))

Open in new window


How can I write this regexp?
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.

käµfm³d 👽Commented:
String pattern = "[A-Za-z]:\\\\.*(?!EXCHANGE)";

Open in new window


If that doesn't work, change the backslashes to this:

String pattern = "[A-Za-z]:\\.*(?!EXCHANGE)";

Open in new window


I always forget how Java prefers them.
0
Terry WoodsIT GuruCommented:
To match a literal backslash in Java, 4 backslashes are needed. To escape a pattern character with a backslash, 2 are needed.

@kaufmed, your pattern didn't work for me when I tested it, but a slight change to it fixed the problem:

String pattern = "[A-Za-z]:\\\\.*?(?=EXCHANGE)";

Open in new window


Please split points with kaufmed if you accept my answer; I used his pattern as a starting point.
0
TolgarAuthor Commented:
@TerryAtOpus: Sorry. I didn't see your answer rating the previous answer.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Terry WoodsIT GuruCommented:
@Tolgar, which solution actually solved your problem? It's easy enough to get the question reopened if necessary.
0
TolgarAuthor Commented:
Actually, I used my own solution which is:

(?<=(-f|-d)).+?(?=(?:EXCHANGE))

Open in new window


If you prefer, I am ok to re-rate the two solutions.
0
CEHJCommented:
        final String ROOT_PAT = ".*([a-zA-Z]:\\\\[^\\\\]*?\\\\).*";
        arg = arg.replaceAll(ROOT_PAT, "$1");

Open in new window

is something you can try
0
Terry WoodsIT GuruCommented:
I'm not worried about getting it reopened then.

Note that this pattern should work exactly the same as your one, and is a little simpler:
(?<=-[fd]).+?(?=EXCHANGE)

Open in new window

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
käµfm³d 👽Commented:
I suggest reopening the question. I didn't have time to test the pattern (which is apparently flawed). If it doesn't work, then my answer isn't the right answer  = )
0
TolgarAuthor Commented:
I am fine. We can reopen the question. But I don't know how?
0
Terry WoodsIT GuruCommented:
I've made the request through the Request Attention link that is at the bottom of the question.
0
CEHJCommented:
:)
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.