• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

Java Regular expression

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
sunnybrad
Asked:
sunnybrad
  • 3
1 Solution
 
for_yanCommented:
hope this should work:

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

I'll now test
0
 
for_yanCommented:
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
 
sunnybradAuthor Commented:
Hi for_yan:

I will try it out and let you know.

Best Regards,

-Sunnybrad
0
 
for_yanCommented:
Sure, please, try it and let me know.
0
 
CEHJCommented:
Yes, but you don't need the character class

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

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now