Link to home
Start Free TrialLog in
Avatar of sunnybrad
sunnybrad

asked on

Java Regular expression

Hi:

I need Java regular expression help. In a String value I want to extract a substring before the appearance of -d means appearance of first hyphen and digit.

For example:

Test-Test2-2.1.2

I want Test-Test2 value to be extracted.

Look forward to your near response.

Best Regards,

-Sunnybrad
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America 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
This worked for me:

      String stest = "Test-Test2-2.1.2";

        stest = stest.replaceAll("-[\\d].*","");

        System.out.println(stest);

Open in new window




Test-Test2

Open in new window

Avatar of sunnybrad
sunnybrad

ASKER

Hi for_yan:

I will try it out and let you know.

Best Regards,

-Sunnybrad
Sure, please, try it and let me know.
Avatar of CEHJ
Yes, but you don't need the character class

s = s.replaceAll("-\\d.*","")

Open in new window