krn
krn

Reputation: 6815

Ruby regex: remove first name, leave last name

I am parsing a text and I want to ignore people's first names.

Examples (cases):

I manage to write this working Ruby regex:

"B.Obama".gsub(/\p{L}+\.(\p{L}+)/, '\\1')

However, it solves only one case. Also, it doesn't check, if the first letter is capital.

So, how should the regex, which combines all these cases, look like?

Details: Ruby 1.92 and UTF-8 strings.

Upvotes: 0

Views: 1852

Answers (2)

Valadas
Valadas

Reputation: 121

I Gave a it a little bit more thought and I like this better:

/^(\w+)[ .,](.+$)/

This will capture both the first name and last name in different capturing groups i.e.

"Mark del cato".scan /^(\w+)[ .,](.+$)/

see rubular for example: Rubular

Or Try

^[^ .]+

This will pick up the first word on a line. that is not delimited by a dot or a space. Hope it helps, see example at Rubular

Upvotes: 1

El Ronnoco
El Ronnoco

Reputation: 11922

Try

(\w+)$

\w+ matches one or more 'word' characters.

The $ is a zero-length match matching the end of the string.

Do you want to be able to pull second names from a piece of text? That could get very difficult. Can you post an excerpt of your text?

Upvotes: 0

Related Questions