Solved

Oracle POSIX regular expressions.

Posted on 2013-06-11
5
362 Views
Last Modified: 2013-06-16
Col1            Col1
tableA            tableB
VARCHAR            NUMBER
1            1
2            2
2a
2;
2?


Query Failure as expected
select *
from taba a, tabb b
where a.col1 = b.col1;

ORA-01722: invalid number
01722. 00000 -  "invalid number"
*Cause:    
*Action:


Following gives me all rows including 1 & 2 which are digits, why?
select * from taba
where regexp_like(col1, '[[:alnum:]]');

Following gives me all rows including 1 & 2 which are digits, why?
select * from taba
where regexp_like(col1, '[[:punct:]]*');


How do i search special characters in a string like say --inverted ? or a chinese character --can't print here.
This does not qualify any any of the above.

[:digit:]      Only the digits 0 to 9
[:alnum:]      Any alphanumeric character 0 to 9 OR A to Z or a to z.
[:alpha:]      Any alpha character A to Z or a to z.
[:blank:]      Space and TAB characters only.
[:xdigit:]     Hexadecimal notation 0-9, A-F, a-f.
[:punct:]      Punctuation symbols
0
Comment
Question by:gram77
[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
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39237435
Following gives me all rows including 1 & 2 which are digits, why?
select * from taba
where regexp_like(col1, '[[:alnum:]]');
Because of how you structured the pattern (and its a REGEXP_LIKE). If a an alphabetic or numeric (i.e. "alnum") occurs *anywhere* within the target column's value, then a match will be declared.

Following gives me all rows including 1 & 2 which are digits, why?
select * from taba
where regexp_like(col1, '[[:punct:]]*');
You are looking for any punctuation mark. However, you marked this as "zero or more" (i.e. * ). This means that you will trivially match any column's value, since any column can have zero or more punctuation marks.

How do i search special characters in a string like say --inverted ? or a chinese character --can't print here.
I'm not certain of this, but you might try:

...where regexp_like(col1, '[^[:alpha:]]');

Open in new window

0
 

Author Comment

by:gram77
ID: 39237888
--why does the following show me 2a, 2? and 2; these are not digits.
How can i search for only digits using posix
select *
from taba
where regexp_like (col1, '[[:digit:]]');
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39238498
"2" is a digit.

I believe your confusion is that you confusing "digits" with "numbers". "2a" is not a number, but it does contain a digit--2. That is why your pattern matches. To match a number you would do something like:

select *
from taba
where regexp_like (col1, '^[[:digit:]]+$');

Open in new window


Where ^ matches the start of a string, $ matches the end of a string, and + means "one or more" of the thing to the left, which in this case is "any digit" ( [[:digit:]] ).
0
 

Author Comment

by:gram77
ID: 39240158
As we have the following character classes,
[:digit:]      Only the digits 0 to 9
[:alnum:]      Any alphanumeric character 0 to 9 OR A to Z or a to z.
[:alpha:]      Any alpha character A to Z or a to z.
[:blank:]      Space and TAB characters only.
[:xdigit:]     Hexadecimal notation 0-9, A-F, a-f.
[:punct:]      Punctuation symbols

Q1. I want to search for anything that is not a digit, alphabet, punctuation, or blank
Q2. I want to search for special characters in the data like - inverted ? or say chinese chracters
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39240814
A1
[^[:alnum:][:punct:][:blank:]]

Open in new window


A2
You can try:
[^[:digit:][:alnum:][:alpha:][:blank:][:xdigit:][:punct:]]

Open in new window

0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

687 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