Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / Oracle / Oracle Server / April 2008

Tip: Looking for answers? Try searching our database.

regexp_substr help, please

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Doug Miller - 29 Apr 2008 16:34 GMT
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?
md - 29 Apr 2008 17:33 GMT
> 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.
[quoted text clipped - 11 lines]
>
> 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$
Malcolm Dew-Jones - 29 Apr 2008 17:46 GMT
: 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
md - 29 Apr 2008 17:52 GMT
On Apr 29, 12:43 pm, yf...@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
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.
[quoted text clipped - 8 lines]
>
> [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 - 29 Apr 2008 17:56 GMT
>: 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.
[quoted text clipped - 8 lines]
>
>[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.
Peter Nilsson - 30 Apr 2008 04:17 GMT
> I need to be able to pull just the last name out of a string
> consisting of lastname and firstname, separated by a
[quoted text clipped - 7 lines]
> 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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.