regexnoob75
regexnoob75

Reputation: 15

Regex for replacing letters next to numbers uppercase

I have this regex (\w+) replace with \u$0

This makes first letter caps for example: james1 to James1.

But I need a regex to make the first letter caps of each word when it starts with a number for example

12james

1azz4ds

1995brandon


666metal

to

12James

1Azz4ds

1995Brandon


666Metal

How do I solve this problem?

Upvotes: 1

Views: 177

Answers (2)

Emma
Emma

Reputation: 27723

Here, we can also collect the digits, then letters maybe both upper or lowercase, and replace it:

[0-9]+([A-Za-z])

enter image description here

We will be adding a start char to capture only those letters that we wish to replace:

^[0-9]+([A-Za-z])

or:

^([0-9]+)([A-Za-z])

and for this expression our replacement would look like to something similar to:

$1\u$2

enter image description here

RegEx

If this expression wasn't desired, it can be modified or changed in regex101.com.

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Upvotes: 1

The fourth bird
The fourth bird

Reputation: 163277

You could match a word boundary \b, match 1+ digits \d+ and then forget what is matched using \K. Then match a single lowercase a-z:

\b\d+\K[a-z]

Replace with:

\u$0

See a Regex demo

If there can be not a non whitespace before the digits, instead of using \b you might also use:

(?<!\S)\d+\K[a-z]

See another Regex demo

Upvotes: 0

Related Questions