Skip to main content
massimod21661912
Participant
September 25, 2018
Answered

Grep function changes "Name Surname" in "N. Surname"

  • September 25, 2018
  • 1 reply
  • 651 views

I'm looking for Grep function that changes (in a list of 100 names and surnames) the name with the name initial pointed.
Example

Robert Redford >> R. Redford

Tom Cruise >> T. Cruise

Thank you!

This topic has been closed for replies.
Correct answer Jongware

Good! That makes it pretty simple!

There are various ways to do this, but I would go for replacing all lowercase characters that follow an uppercase and are followed by space, an uppercase and at least one lowercase, with a full stop.

Let's break that down:

all lowercase characters → \l+ (that's a lowercase 'ell', which stands for "lowercase")

.. that follow an uppercase → (?<=\u)\l+ (this is a lookbehind, which checks if there is an uppercase character immediately before what we just found)

.. and are followed by space, uppercase, lowercase → (?<=\u)\l+(?= \u\l) (which is a lookahead that verifies the space, uppercase, and lowercase)

If you use "find", you will see only the series of lowercase in the first name will be selected, and so all you need to replace it with is a single full stop itself.

1 reply

Jongware
Community Expert
Community Expert
September 25, 2018

Do you also have names such as "Vernon T. Waldrip"? "L. Ron Hubbard"? "Dr. Richard Kimble"?

massimod21661912
Participant
September 26, 2018

No, only simple "Name Surname":

"Vernon Waldrip"

"Ron Hubbard"

"Richard Kimble"

massimod21661912
Participant
September 26, 2018

Good! That makes it pretty simple!

There are various ways to do this, but I would go for replacing all lowercase characters that follow an uppercase and are followed by space, an uppercase and at least one lowercase, with a full stop.

Let's break that down:

all lowercase characters → \l+ (that's a lowercase 'ell', which stands for "lowercase")

.. that follow an uppercase → (?<=\u)\l+ (this is a lookbehind, which checks if there is an uppercase character immediately before what we just found)

.. and are followed by space, uppercase, lowercase → (?<=\u)\l+(?= \u\l) (which is a lookahead that verifies the space, uppercase, and lowercase)

If you use "find", you will see only the series of lowercase in the first name will be selected, and so all you need to replace it with is a single full stop itself.


Perfect explanation! Great!!!

Thank you very much!