Search code examples
javaregexreplaceall

Java replaceAll() to pull numbers and periods


I have a line of output similar to "Spec-Version: 11.3.0". I'm struggling to pull only the version out, with periods, using replaceAll(). Right now I have something like this:

version = line.replaceAll("[\\D+\\.]" , "");

With this I'm getting a version of:

1130

No matter what combination of syntax I use I'm either losing the periods or pulling the entire line.

Any help is appreciated.


Solution

  • The below regex would store the version number in the first group. Replace the whole string with the first group.

    :\s*(.*$)
    

    Your java string would be ":\\s*(.*$)"

    DEMO