[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java Regular expression

Posted on 2012-03-17
5
Medium Priority
?
313 Views
Last Modified: 2012-03-23
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
0
Comment
Question by:sunnybrad
  • 3
5 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 37733445
hope this should work:

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

I'll now test
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37733448
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

0
 

Author Comment

by:sunnybrad
ID: 37733473
Hi for_yan:

I will try it out and let you know.

Best Regards,

-Sunnybrad
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37733479
Sure, please, try it and let me know.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37733488
Yes, but you don't need the character class

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

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Make the most of your online learning experience.
Simple Linear Regression
Six Sigma Control Plans
Suggested Courses
Course of the Month17 days, 15 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question