Solved

need help understanding REGEXP_REPLACE in Oracle

Posted on 2014-11-17
18
336 Views
Last Modified: 2014-11-18
I've been slowing letting REGEXP in Oracle sink in, from several recent and helpful EE posts.

For these two examples, they seem to me like they do the same but there are exact opposite results:

for 'SMI$TH' this
SELECT REGEXP_REPLACE(UPPER('&cust_last_name') , '[^A-Za-z]', '') AS v_cust_last_name FROM dual;
returns 'SMITH'
- doesn't this mean: "Starting at the beginning of the string (^), find any characters in A-Z or a-z and replace it with nothing." Which would mean result = $, but obviously it's the OPPOSITE of that.

for 'SMI$TH' this
SELECT REGEXP_REPLACE(UPPER('&cust_last_name') , '[[:alpha:]]', '') AS v_cust_last_name FROM dual;
returns $
- this means to me: "Find any alpha in  the string and replace it with nothing". And so it does.

so I must be missing the proper interpretation of the first example . . .
0
Comment
Question by:Gadsden Consulting
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
18 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 40448130
>> '[^A-Za-z]'

Since the '^' is inside the backets it means 'NOT',  So that replaces anything NOT upper or lower case letters.

When '^' is outside the backets it is beginning of line.

>>Find any alpha in  the string and replace it with nothing".

Correct.
0
 

Author Comment

by:Gadsden Consulting
ID: 40448179
slightvw,

makes perfect sense, and answers a question I had a while back, how to say "not this pattern", but I never posted the question.

I checked my book first, and all I saw was "^ = matches the position at the start of the string".

What do the brackets mean ?
0
 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 500 total points
ID: 40448193
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:Gadsden Consulting
ID: 40448199
Great ! thanks a lot, that's also not explained in the book . . .
0
 

Author Comment

by:Gadsden Consulting
ID: 40448204
It just seems the " ^ " should be outside the brackets, but oh well.
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40448216
>>It just seems the " ^ " should be outside the brackets, but oh well.

It 'should' when you want the beginning of the line.

When dealing with character classes, it is inside and means 'NOT'.

Consider it an 'overloaded' operator...

I bet it is in your book somewhere...
0
 

Author Comment

by:Gadsden Consulting
ID: 40448242
>>I bet it is in your book somewhere...
- I've studied this inside and out, I don't see it. He's got 7 pages on REGEXP, compared to the 600+ page time your or sdstuber recommended. Plus the tutorial you just linked is a bit longer than 7 pages.

But good on this, thanks.
0
 
LVL 12

Expert Comment

by:Praveen Kumar Chandrashekatr
ID: 40448334
The '^' is acted as NOT inside the braces as Slightwv stated in his post.

[^A-Za-z] matches any single character that is not a upper case letter "A" to "Z" or lowercase letter from "a" to "z".

SELECT REGEXP_REPLACE(UPPER('&cust_last_name') , '[A-Za-z]', '') AS v_cust_last_name FROM dual;

these will return $ as output.

here is regular expression details.

http://en.wikipedia.org/wiki/Regular_expression
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40448357
>>- I've studied this inside and out, I don't see it.  He's got 7 pages on REGEXP

You need a better 'book'...  ;)

Remember the O'Reilly book I suggest in a previous question or was that a different Member? If not:  I like the O'Reilly book on regular expressions.  It covers some aspects of regular expressions that Oracle doesn't allow but still a really good book.

There is WAY too much to cover on regular expression than can fit in 7 pages.  That is why there are entire books just on them not just a section in a single book.
0
 

Author Comment

by:Gadsden Consulting
ID: 40448713
praveencpk, thanks for the tips. sorry I had already awarded points.

>>Remember the O'Reilly book I suggest in a previous question
- yes, I do. I just don't think 600 pages is needed for Regular Expression for Oracle. Maybe 20 ? or 50 ? but 600 ???

>>There is WAY too much to cover on regular expression than can fit in 7 pages.
- as I now see. The author should make reference to it, but it just presents it like it's substr and then moves along.

I've been programming for a while, mostly in Oracle. Nothing fancy, but I've always been able to figure out something with little effort, as it normally makes "sense", like good programming should. But this RegExp is like the geeks gone wild, kind of like when you had to give hieroglyphics to print landscape in Lotus123. It shouldn't be that complicated. But I'll keep studying.

If there's an Oracle book, like 100 pages or so, that's what I would go for.
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40448760
>>But this RegExp is like the geeks gone wild

Anything powerful is complex.

Buy the O'reilly book and skip every 3rd page?

You don't need to read it word for word.  Just scan it to get the 'basics' that will trigger a "I think I read that" response when you stumble.

To be honest, I never made it ALL the way through it.  I started scanning about half the way through.

Want to see how far down the rabbit hole you can go?
Have you looked at some of the examples for something you would think is simple:  Validate an IPV4 address.

Note: Not everything on this site is Oracle Compatible but most things can be 'ported'.
http://www.regexlib.com/Search.aspx?k=IP+address&AspxAutoDetectCookieSupport=1

Want to double down?  Try email addresses...

There are some good ones on the regexlib site!!!!!
0
 

Author Comment

by:Gadsden Consulting
ID: 40448782
slightvw,

>>Anything powerful is complex.
- like love ???

>>Buy the O'reilly book and skip every 3rd page?
- but the price isn't 2/3rds . . .

>>Validate an IPV4 address.
- maybe next week, but good point.

>>Want to double down?  Try email addresses...
- good exercise, but hasn't someone already done that ??? that's the method I want . . . :-)
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40448805
>>- maybe next week,
>>- good exercise

I didn't mean for you to 'try' it as an exercise.  I meant look at the ones posted on regexlib to get an idea of how complex what seems 'simple' can be.

But feel free to try one of your own before you go looking to see what others have posted.

>>- but the price isn't 2/3rds . . .

There is some online stuff and I remember sdstuber pointed you to a website that had good references but...

$20 with shipping used on Amazon.  How many hours would you need to find 'decent' stuff online that may or may not be correct then try to absorb what it was telling you versus the hours to sit down with a good book.

Starting out:  The Oracle online docs and most websites are HORRIBLE for learning.  They are good for reference or helping fill in the gaps but you need to basics to know where to look.

>>- like love ???
*blushes*  Sorry but I'm spoken for....  Can we just be friends?
;)   KIDDING!!!!!!
0
 

Author Comment

by:Gadsden Consulting
ID: 40448819
all good, thanks.

I did find this, but it's Oracle 10g / 2003. Seems like someone needs to do an update.

>>Can we just be friends?
of course ! I'm past my prime, anyway . . .
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40448938
>>I did find this,

Cannot review that one.  I prefer O'Reilly books so it probably isn't a waste of $$$.  Just never seen it. Mastering (the Owl), I own so I can suggest it.

All I can say I don't think that regular expressions have made major leaps in recent years.  Sort of like basic Unix commands:  If you know them from 10 years ago, you are probably safe today.  Should be the same with regex.  I wouldn't be afraid of learning the basics from something that old.

Then again, I've not kept up on regex trends.

The last time I had a complex regex question I asked it here in the Perl Topic Area.  The Perl folks tend to be the best that I've found when it comes to regex.

The problem with that is you'll need to have the basics because Perl regex isn't 100% compatible with Oracle's.  I had to port what they gave me and learn to ask in translated Perl syntax.

It was an interesting question...
0
 

Author Comment

by:Gadsden Consulting
ID: 40449582
I posted the wrong one, here's the 66-page book that looks good, but only through 10g. I will probably start with this. I did notice, however, that Regexp_Count is new to 11g.

 Thx.
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40449697
The problem with that book is it is a 'reference'.  Might as well stick to the Oracle docs and online references.

For example:  You will see the '^' in the index with a few pages that talk about it and might see a simple example but it probably won't explain much behind it.
0
 

Author Comment

by:Gadsden Consulting
ID: 40449704
Ok, sounds good, thx.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question