Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Mailing Lists -> Oracle-L -> Regular Expression confusion
I'm getting confused on regular expressions (Oracle 10.1.0.4). I want to
strip all punctuation from a user supplied input string except for the '%'
wildcard character. To see if my formatting of the syntax was correct, I
wrote the following sql statement:
SQL> set escape off
SQL> select UPPER(regexp_replace(name,'[''''|"|;| |-|,|.]')) test_data
2 from names
3 where upper(regexp_replace(name,'[[:punct:][:space:]]'))
4 like ('%JGO%');
TEST_DATA
Not quite what I wanted, as a hyphen still appears.
So, I tried escaping the hyphen:
SQL> select UPPER(regexp_replace(name,'[''''|"|;| |\-|,|.]')) test_data
2 from names
3 where upper(regexp_replace(name,'[[:punct:][:space:]]'))
4 like ('%JGO%');
TEST_DATA
Definitely not what I wanted! So, I figured I'd try adding an extra set of brackets (like the second regexp_replace uses):
SQL> select UPPER(regexp_replace(name,'[[''''|"|;| |\-|,|.]]'))
SQL> test_data
2 from names
3 where upper(regexp_replace(name,'[[:punct:][:space:]]'))
4 like ('%JGO%');
TEST_DATA
A. J. GOLD MINE A. J. GOLD MINING CO. A.J. GOLD MINING CO.
That had the effect of negating what I wanted to accomplish in the first place! I might as well have just selected name.
And if I changed the first regexp_replace to UPPER(regexp_replace(name,'[''''|"|;| |'-'|,|.]')), Then I get an ORA-01722 invalid number.
How do I modify my first regexp_replace to also strip out the hyphen while allowing the '%' wildcard, unlike the :punct: class?
![]() |
![]() |