Solved

Oracle POSIX regular expressions.

Posted on 2013-06-11
5
356 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
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.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

726 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