Skip to content
Advertisement

Regex for extracting all heading digits from a string

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

JavaScript

Advertisement

Answer

Use a word boundary b:

JavaScript

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 ^:

JavaScript

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement