Solved

Java Regular expression

Posted on 2012-03-17
5
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 500 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Progress

691 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