Solved

Java Regular expression

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

867 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

23 Experts available now in Live!

Get 1:1 Help Now