Solved

Oracle POSIX regular expressions.

Posted on 2013-06-11
5
326 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
  • 3
  • 2
5 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
--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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
"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
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

744 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

17 Experts available now in Live!

Get 1:1 Help Now