Home > Software engineering >  Regex for extracting all heading digits from a string
Regex for extracting all heading digits from a string

Time:01-18

I am trying to extract all heading digits from a string using Java regex without writing additional code and I could not find something to work:

"12345XYZ6789ABC" should give me "12345".
"X12345XYZ6789ABC" should give me nothing

public final class NumberExtractor {
    private static final Pattern DIGITS = Pattern.compile("what should be my regex here?");

    public static Optional<Long> headNumber(String token) {
        var matcher = DIGITS.matcher(token);
        return matcher.find() ? Optional.of(Long.valueOf(matcher.group())) : Optional.empty();
    }
}

CodePudding user response:

Use a word boundary \b:

\b\d 

See live demo.

If you strictly want to match only digits at the start of the input, and not from each word (same thing when the input contains only one word), use ^:

^\d 

Pattern DIGITS = Pattern.compile("\\b\\d "); // leading digits of all words
Pattern DIGITS = Pattern.compile("^\\d "); // leading digits of input

CodePudding user response:

I'd think something like "^[0-9]*" would work. There's a \d that matches other Unicode digits if you want to include them as well.

Edit: removed errant . from the string.

  •  Tags:  
  • Related