Skip to content
Advertisement

Regex for matching all numbers and every first letter in words

I’m trying to define a regex which matches all numbers and only the first letter of each word in a string. The word can be preceded by an empty space or by a special character.

Example strings and matches:

1234 Something another thing - 1234Sat
569 Camel case is Important - 569CciI
123&else - 123e
A word here and numbers 1234 - Awhan1234
Someone 0987 and string - S0987as

I’m currently at this point:

d*b([a-zA-Z])

Which doesn’t quite work as needed. An explanation of the proposed solution would also be really helpful for me.

Advertisement

Answer

As per my comment, change to:

d|b([a-zA-Z])

This is using the | as an or operator looking for digits or letters just after a word boundary.

See the online demo.

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