Solved

Java Regular expression

Posted on 2012-03-17
5
300 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 500 total points
Comment Utility
hope this should work:

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

I'll now test
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
Comment Utility
Hi for_yan:

I will try it out and let you know.

Best Regards,

-Sunnybrad
0
 
LVL 47

Expert Comment

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

Expert Comment

by:CEHJ
Comment Utility
Yes, but you don't need the character class

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

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
A short article about a problem I had getting the GPS LocationListener working.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

763 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now