Solved

Oracle POSIX regular expressions.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
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.

912 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

16 Experts available now in Live!

Get 1:1 Help Now