Solved

need help understanding REGEXP_REPLACE in Oracle

Posted on 2014-11-17
18
324 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
  • 9
  • 8
18 Comments
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
Comment Utility
>> '[^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
Comment Utility
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 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 500 total points
Comment Utility
0
 

Author Comment

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

Author Comment

by:Gadsden Consulting
Comment Utility
It just seems the " ^ " should be outside the brackets, but oh well.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
>>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
Comment Utility
>>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:praveencpk
Comment Utility
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 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
>>- 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Gadsden Consulting
Comment Utility
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 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
>>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
Comment Utility
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 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
>>- 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
Comment Utility
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 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
>>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
Comment Utility
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 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
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
Comment Utility
Ok, sounds good, thx.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now