need help understanding REGEXP_REPLACE in Oracle

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 . . .
Gadsden ConsultingIT SpecialistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
>> '[^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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gadsden ConsultingIT SpecialistAuthor Commented:
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
slightwv (䄆 Netminder) Commented:
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Gadsden ConsultingIT SpecialistAuthor Commented:
Great ! thanks a lot, that's also not explained in the book . . .
0
Gadsden ConsultingIT SpecialistAuthor Commented:
It just seems the " ^ " should be outside the brackets, but oh well.
0
slightwv (䄆 Netminder) Commented:
>>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
Gadsden ConsultingIT SpecialistAuthor Commented:
>>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
Praveen Kumar ChandrashekatrDatabase Analysist Senior Commented:
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
slightwv (䄆 Netminder) Commented:
>>- 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
Gadsden ConsultingIT SpecialistAuthor Commented:
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
slightwv (䄆 Netminder) Commented:
>>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
Gadsden ConsultingIT SpecialistAuthor Commented:
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
slightwv (䄆 Netminder) Commented:
>>- 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
Gadsden ConsultingIT SpecialistAuthor Commented:
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
slightwv (䄆 Netminder) Commented:
>>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
Gadsden ConsultingIT SpecialistAuthor Commented:
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
slightwv (䄆 Netminder) Commented:
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
Gadsden ConsultingIT SpecialistAuthor Commented:
Ok, sounds good, thx.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.