Unix Technical Forum

regexp_substr help, please

This is a discussion on regexp_substr help, please within the Oracle Miscellaneous forums, part of the Oracle Database category; --> I need to be able to pull just the last name out of a string consisting of lastname and ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database > Oracle Miscellaneous

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-29-2008, 09:28 PM
Doug Miller
 
Posts: n/a
Default regexp_substr help, please

I need to be able to pull just the last name out of a string consisting of
lastname and firstname, separated by a comma, or space, or comma and space.
Complicating matters somewhat is the fact that lastname might be something
like "Mc Kay" or "St. Louis" so simply grabbing everything before the first
space isn't sufficient.

The closest I've come so far is
select regexp_substr ('St. Louis, Ted', '.{4}[A-Z]+') from dual;
but this returns only
St. L

I thought regular expression matching was supposed to be "greedy", and take as
many characters as would satisfy the pattern ("St. Louis" in this case).

What am I doing wrong?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-29-2008, 09:28 PM
md
 
Posts: n/a
Default Re: regexp_substr help, please

On Apr 29, 10:34 am, spamb...@milmac.com (Doug Miller) wrote:
> I need to be able to pull just the last name out of a string consisting of
> lastname and firstname, separated by a comma, or space, or comma and space.
> Complicating matters somewhat is the fact that lastname might be something
> like "Mc Kay" or "St. Louis" so simply grabbing everything before the first
> space isn't sufficient.
>
> The closest I've come so far is
> select regexp_substr ('St. Louis, Ted', '.{4}[A-Z]+') from dual;
> but this returns only
> St. L
>
> I thought regular expression matching was supposed to be "greedy", and take as
> many characters as would satisfy the pattern ("St. Louis" in this case).
>
> What am I doing wrong?


These are you combos?

ln,fn|ln, fn|ln fn
l n,fn|l n, fn|l n fn

I'd start by nuking the fn. It's the last solid string and it has an
nice anchor fn$


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-29-2008, 09:28 PM
Doug Miller
 
Posts: n/a
Default Re: regexp_substr help, please

In article <4817502b$1@news.victoria.tc.ca>, yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones) wrote:
>Doug Miller (spambait@milmac.com) wrote:
>: I need to be able to pull just the last name out of a string consisting of
>: lastname and firstname, separated by a comma, or space, or comma and space.
>: Complicating matters somewhat is the fact that lastname might be something
>: like "Mc Kay" or "St. Louis" so simply grabbing everything before the first
>: space isn't sufficient.
>
>: The closest I've come so far is
>: select regexp_substr ('St. Louis, Ted', '.{4}[A-Z]+') from dual;
>: but this returns only
>: St. L
>
>[A-Z] doesn't match o


Yeah, I just got back from lunch and realized the same thing. Knew it had to
be something stupid like that. Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-29-2008, 09:28 PM
Malcolm Dew-Jones
 
Posts: n/a
Default Re: regexp_substr help, please

Doug Miller (spambait@milmac.com) wrote:
: I need to be able to pull just the last name out of a string consisting of
: lastname and firstname, separated by a comma, or space, or comma and space.
: Complicating matters somewhat is the fact that lastname might be something
: like "Mc Kay" or "St. Louis" so simply grabbing everything before the first
: space isn't sufficient.

: The closest I've come so far is
: select regexp_substr ('St. Louis, Ted', '.{4}[A-Z]+') from dual;
: but this returns only
: St. L

[A-Z] doesn't match o
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-29-2008, 09:28 PM
md
 
Posts: n/a
Default Re: regexp_substr help, please

On Apr 29, 12:43 pm, yf...@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
wrote:
> Doug Miller (spamb...@milmac.com) wrote:
>
> : I need to be able to pull just the last name out of a string consisting of
> : lastname and firstname, separated by a comma, or space, or comma and space.
> : Complicating matters somewhat is the fact that lastname might be something
> : like "Mc Kay" or "St. Louis" so simply grabbing everything before the first
> : space isn't sufficient.
>
> : The closest I've come so far is
> : select regexp_substr ('St. Louis, Ted', '.{4}[A-Z]+') from dual;
> : but this returns only
> : St. L
>
> [A-Z] doesn't match o


Here a perl test I did to check out a maybe.
@ is an array of your combos (did I get them all?)
The loop goes thru each. $1 will contain the "last name".


@s = ("mc winter, first",
"mc. winter, first",
"winter, first",
"mc winter,first",
"mc. winter,first",
"winter,first",
"mc winter first",
"mc. winter first",
"winter first",
);

foreach $x (@s) {
print qq/look at "$x"\n/;
{
$x =~ /(.*)?[, ]{1}[a-zA-Z]*$/;
print $1 . "\n";
}
}

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 05-02-2008, 06:05 AM
Peter Nilsson
 
Posts: n/a
Default Re: regexp_substr help, please

Doug Miller wrote:
> I need to be able to pull just the last name out of a string
> consisting of lastname and firstname, separated by a
> comma, or space, or comma and space.
> Complicating matters somewhat is the fact that lastname
> might be something like "Mc Kay" or "St. Louis" so simply
> grabbing everything before the first space isn't sufficient.
>
> The closest I've come so far is
> select regexp_substr ('St. Louis, Ted', '.{4}[A-Z]+') from dual;
> but this returns only
> St. L


Start with a 'student' aproach...

with
names as
(
select 'mc winter, first' nme from dual union all
select 'mc. winter, first' nme from dual union all
select 'winter, first second' nme from dual union all
select 'mc winter,first' nme from dual union all
select 'mc. winter,first' nme from dual union all
select 'winter,first' nme from dual union all
select 'mc winter first second' nme from dual union all
select 'mc. winter first' nme from dual union all
select 'winter first' nme from dual union all
select 'macwilliams' nme from dual
)
select
nme,
regexp_replace
(
trim(nme),
'^'
|| '(' -- with comma
|| '([^,]*)' -- surname
|| ', *'
|| '(.*)' -- given name(s)
|| ')'
|| '|'
|| '(' -- without comma
|| '(' -- surname
|| '((mc|st)\.?)? *' -- optional prefix
|| '[^ ]+'
|| ')'
|| ' *'
|| '(.*)' -- given name(s)
|| ')'
|| '$',
'"\2\5", "\3\8"', -- "surname", "given"
1, 0, 'i' -- case insensitive
)
from
names

--
Peter
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:04 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com