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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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? |
| |||
| 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$ |
| |||
| 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. |
| |||
| 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 |
| |||
| 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"; } } |
| ||||
| 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 |