Select last name except First Initial

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Select last name except First Initial

Postby RonH » Wed Jun 06, 2012 9:55 pm

Right now I have a list of first and Last names, one name per row. Each name is separated by a space, I want to keep the Fist letter of the last name and erase the rest and place a period at the end.

i.e. Tom Smith -> Tom S.

Ron
RonH
Forum Newbie
 
Posts: 6
Joined: Thu Mar 01, 2012 3:39 pm

Re: Select last name except First Initial

Postby requinix » Wed Jun 06, 2012 10:13 pm

Sounds reasonable. What's your question?

Just in case it's "will somebody give me the answer?" then allow me to reply preemptively with "no, but we'll help you if you're having problems with your own attempt at it".
User avatar
requinix
Spammer :|
 
Posts: 5610
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Select last name except First Initial

Postby Christopher » Wed Jun 06, 2012 10:18 pm

There are several ways to do this. Here is one way: Use strpos() to find the position of space and then substr() to truncate the string.
(#10850)
User avatar
Christopher
Site Administrator
 
Posts: 12681
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Select last name except First Initial

Postby RonH » Wed Jun 06, 2012 10:34 pm

Absolutely, I'm not really looking for a hand out I want to understand what I'm doing.

I've tried this
\s\w+\n

But I'm not sure how to start the selection after the space-first character (In the last name).
What I have highlights the space and the last name.

Ron
RonH
Forum Newbie
 
Posts: 6
Joined: Thu Mar 01, 2012 3:39 pm

Re: Select last name except First Initial

Postby RonH » Thu Jun 07, 2012 10:13 pm

I really would like to do this with Regex if I can.

Ron
RonH
Forum Newbie
 
Posts: 6
Joined: Thu Mar 01, 2012 3:39 pm

Re: Select last name except First Initial

Postby requinix » Thu Jun 07, 2012 11:11 pm

Regex isn't the best tool for it but whatever.

Start at the space, capture the next non-space character, and read until the end of the string. Replace that with a space and the character you captured.
User avatar
requinix
Spammer :|
 
Posts: 5610
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Select last name except First Initial

Postby Christopher » Fri Jun 08, 2012 2:54 pm

Christopher wrote:There are several ways to do this. Here is one way: Use strpos() to find the position of space and then substr() to truncate the string.

substr($name, 0, strpos($name, ' ')+2) . '.'
(#10850)
User avatar
Christopher
Site Administrator
 
Posts: 12681
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US


Return to Regex

Who is online

Users browsing this forum: No registered users and 3 guests