Solved

Java Regular expression

Posted on 2012-03-17
5
305 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
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to debug htl and js pages 8 54
Eclipse for Java EE development 2 44
asp Google Map 2 61
Java array sort 10 32
A short article about a problem I had getting the GPS LocationListener working.
This is an explanation of a simple data model to help parse a JSON feed
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

749 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