How create a regular expression to query Oracle? - regex

I have table in Oracle DB. For example tbl1. And 1 column. For Example col1. My col1 - column text type. I need select all rows where text is 0 and It occurs once in the text. And if text It contains >1 digits I do not need it. For example i need this rows:
0
text0
0text
text 0 text
text0 text
and I do not need this rows
only text
0 0
00
10
3243455
0text 1
I think I need regex but I do not know how to use them.
I write
select * from tbl1 t where regexp_like(t.col1,'[0]{1}')
but i get only rows where contains 0

[^0-9]*[0-9][^0-9]*
This means: any non-digit any number of times, then a digit exactly once, and then any non-digit any number of times.
You might need to add ^ and $ to force it to match the entire string:
^[^0-9]*[0-9][^0-9]*$

Based on the answer by Ilya Kogan, you could get the rows you want with the following regular expression:
WITH tbl1 AS (SELECT '0' col1 FROM dual
UNION
SELECT 'text0' col1 FROM dual
UNION
SELECT '0text' col1 FROM dual
UNION
SELECT 'text 0 text' col1 FROM dual
UNION
SELECT 'text0 text ' col1 FROM dual
UNION
SELECT 'only text' col1 FROM dual
UNION
SELECT '0 0' col1 FROM dual
UNION
SELECT '00' col1 FROM dual
UNION
SELECT '10' col1 FROM dual
UNION
SELECT '3243455' col1 FROM dual
UNION
SELECT '0text 1' col1 FROM dual)
SELECT COL1, REGEXP_SUBSTR(col1,'\A\D*0\D*\Z')
FROM tbl1
WHERE REGEXP_LIKE(col1,'\A\D*0\D*\Z')
Where:
\A is the beginning of the line.
\D is a non-digit character.
0 is the character for the number 0.
\Z is the end of the line.

Related

Oracle REGEXP_LIKE logical and matching of substrings in string

I have a string containing codes like 'code1 code2 code3'. It should return the string if all codes entered are contained in the string.
For example:
select * from (
select 'avs cde jkl' code from dual)
where REGEXP_LIKE(code, 'REGEX-MAGIC')
When the regex is now something like ^(?=.*\bjkl\b)(?=.*\bavs\b).*$ then it should return the code. But this syntax is not working for regex in oracle.
The logic is 'if all codes looked for are in the string (order does not matter), then return the code.'
I have researched and this would be achievable with a positive lookahead, but oracle does not support this as far as I know. I would search for one regex and not a construct like REGEXP_LIKE(...,..) and REGEXP_LIKE(...,..) and ....
The Oracle Version is 12c.
Any help would be appreciated!
Oracle does not support look-ahead, look-behind or word boundaries in regular expressions.
If you have the sample data:
CREATE TABLE table_name (code) AS
SELECT 'avs cde jkl' FROM DUAL UNION ALL
SELECT 'avs cde' FROM DUAL UNION ALL
SELECT 'jkl avs' FROM DUAL UNION ALL
SELECT 'cde jkl' FROM DUAL;
Option 1:
The simplest query is to not use regular expressions and to look for sub-string matches using multiple LIKE conditions:
SELECT code
FROM table_name
WHERE ' ' || code || ' ' LIKE '% avs %'
AND ' ' || code || ' ' LIKE '% jkl %'
Which outputs:
CODE
avs cde jkl
jkl avs
Option 2:
You could use (slower) regular expressions with multiple REGEXP_LIKE conditions:
SELECT code
FROM table_name
WHERE REGEXP_LIKE(code, '(^| )avs( |$)')
AND REGEXP_LIKE(code, '(^| )jkl( |$)')
Which outputs the same as above.
Option 3:
You could put the matches into a sub-query factoring clause and then use a LATERAL join:
WITH match_conditions (match) AS (
SELECT 'avs' FROM DUAL UNION ALL
SELECT 'jkl' FROM DUAL
)
SELECT code
FROM table_name t
CROSS JOIN LATERAL (
SELECT 1
FROM match_conditions
WHERE ' ' || code || ' ' LIKE '% ' || match || ' %'
HAVING COUNT(*) = (SELECT COUNT(*) FROM match_conditions)
)
Which outputs the same as above.
Option 4:
If you really want a single regular expression then you can generate each permutation of the codes to match and concatenate them into a single regular expression:
SELECT code
FROM table_name
WHERE REGEXP_LIKE(
code,
'(^| )avs( | .*? )jkl( |$)' -- Permutation 1
|| '|(^| )jkl( | .*? )avs( |$)' -- Permutation 2
)
Which outputs the same as above.
However, this is going to get problematic to maintain as the number of codes to match grows as, for 2 items there are 2 permutations but for 5 items there are 5! = 120 permutations.
Option 5:
You could declare a nested table collection:
CREATE TYPE string_list AS TABLE OF VARCHAR2(20);
Then split the string (again, you do not need slow regular expressions) and then compare it to a nested table:
WITH bounds (rid, code, spos, epos) AS (
SELECT ROWID, code, 1, INSTR(code, ' ', 1)
FROM table_name
UNION ALL
SELECT rid, code, epos + 1, INSTR(code, ' ', epos + 1)
FROM bounds
WHERE epos > 0
)
SEARCH DEPTH FIRST BY code SET order_rn
SELECT MAX(code) AS code
FROM bounds
GROUP BY rid
HAVING string_list('avs', 'jkl') SUBMULTISET OF CAST(
COLLECT(
CAST(
CASE epos
WHEN 0
THEN SUBSTR(code, spos)
ELSE SUBSTR(code, spos, epos - spos)
END
AS VARCHAR2(20)
)
)
AS string_list
);
Depending on the client application you are using, you can pass the entire string_list('avs', 'jkl') collection in as a single bind variable that you can populate from an array. Java (and some languages built on top of Java) using an ODBC driver can do this; C# cannot directly but you can pass an associative array and convert it to a nested table collection with a helper function.
Which outputs the same as above.
db<>fiddle here
I'm not good at regex-magix, but - see if something like this helps.
This is a table that contains those codes:
SQL> select * from codes;
ID CODE
---------- -----------
1 avs cde jkl
2 xyz avs
Query
splits every code into rows (t_split CTE)
does the same for the entered parameter (par_string) value (p_split CTE)
why? So that they could act as if they were rows in a table, and you can apply the MINUS set operator
if MINUS returns nothing, there's a match; otherwise it's a mismatch
SQL> with
2 -- split code to rows
3 t_split as
4 (select id,
5 code original_code,
6 regexp_substr(code, '[^ ]+', 1, column_value) code
7 from codes cross join
8 table(cast(multiset(select level from dual
9 connect by level <= regexp_count(code, ' ') + 1
10 ) as sys.odcinumberlist))
11 where id = &&par_id
12 ),
13 -- split parameter to rows
14 p_split as
15 (select regexp_substr('&&par_string', '[^ ]+', 1, level) code
16 from dual
17 connect by level <= regexp_count('&&par_string', ' ') + 1
18 )
19 --
20 -- if all parameter's "pieces" of code are contained in CODE value, MINUS returns nothing
21 -- so there's a match
22 select distinct t.original_code,
23 '&&par_string' par_string,
24 case when (select count(*)
25 from (select code from t_split
26 minus
27 select code from p_split
28 )
29 ) = 0 then 'Match'
30 else 'Mismatch'
31 end result
32 from t_split t
33 where t.id = &&par_id;
Enter value for par_id: 1
Enter value for par_string: jkl avs cde
ORIGINAL_CO PAR_STRING RESULT
----------- ----------- --------
avs cde jkl jkl avs cde Match
SQL> undefine par_string
SQL> /
Enter value for par_string: avs jkl www
ORIGINAL_CO PAR_STRING RESULT
----------- ----------- --------
avs cde jkl avs jkl www Mismatch
SQL>
Depending on tool you use (this is SQL*Plus), you might need to replace && with a colon :; or, convert such a piece of code to a function.

Regex extract positive or negative number from string oracle

I am struggling in finding the solution to the following problem:
Assuming the character values 512a, -1230b, -2 and 2.
Which can be obtained into a table from the following query:
with my_input_values as (
select '512a' my_val from dual union select '-1230b' my_val from dual union select '2' my_val from dual union select '-2' my_val from dual
)
select * from my_input_values;
I am trying to build the regular expression that extract the number keeping the positive or negative sign from each value.
The expected result are the following numeric values: 512, -1230, 2 and -2.
Which can be obtained into a table through the following query:
with result as (
select 512 my_val from dual union select -1230 my_val from dual union select 2 my_val from dual union select -2 my_val from dual
)
select * from result;
You could match an optional character (or use an asterix * instead of ? to match zero or more characters) followed by a quote. Then you could replace that match with an empty string.
[a-z]?'
For completeness of the answer, the Oracle code to accomplish this task is the following:
select regexp_replace('-asd51assaddasd2a', '[a-z]?') from dual;
which successfully results in a numberic field maintaining the polarity sign
select -512 from dual;

How to update a columns having a specific string patter/sequence

There is a column in my table having values of a pattern like 'A=xxx^B=xxx^C=xxx^D=xxx^' i need to update all the columns having this pattern to a pattern like 'C=xxx^D=xxx^', where x is a number.
Would something like this help? REGEXP_LIKE returns rows which satisfy the condition, while an ordinary SUBSTR returns the desired result.
SQL> with test (col) as
2 (select 'A=123^B=123^C=123^D=123^' from dual union
3 select 'A=123^B=456^C=789^D=987^' from dual union
4 select 'A=333^C=333^D=333^' from dual union
5 select 'C=987^D=987^' from dual union
6 select 'B=876^' from dual union
7 select 'A=123^B=123^C=123^D=123^E=123^' from dual
8 )
9 select col,
10 substr(col, instr(col, 'C')) result
11 from test
12 where regexp_like(col, '^A=\d+{3}\^B=\d+{3}\^C=\d+{3}\^D=\d+{3}\^$');
COL RESULT
------------------------------ ------------------------------
A=123^B=123^C=123^D=123^ C=123^D=123^
A=123^B=456^C=789^D=987^ C=789^D=987^
SQL>
I managed to come up with a solution since i'm looking for a pattern which starts from 'A=' i used REGEXP_LIKE to find that particular pattern. Then i used SUBSTR to extract the value from the string which should start from the 2nd '^' character.
Update MYTABLE t set t.key = SUBSTR(t.key,INSTR(t.key,'^',1,2)+1) WHERE REGEXP_LIKE(t.key_ref,'^A=') and t.dno = 'xxxxx';

Converting to number from string with special characters

I have a input column with below values.
1.2%
111.00$
$100.00
aa
ss
Output expected
1.2
111.00
100.00
null
null
I am trying to use REGEXP_REPLACE and tried replacing every character that is not a digit or "." so that 1.2% will become 1.2.
Here is the query I tried but this didn't work.
regexp_replace('%1.aa2', '[^[\d|\.]]', '')
Can anyone suggest how to do that? and what I am doing wrong? I am working on Oracle 11.2 database with pl/sql developer.
Use POSIX class for matching digit chars ie [:digit:]. So this negated class [^[:digit:].] should match any character but not of dot or digit.
regexp_replace('%1.aa2', '[^[:digit:].]', '')
You can use the [^0-9.] regex and replace with empty string:
with testdata(txt) as (
select 'ss' from dual
union
select 'aa' from dual
union
select '$100.00' from dual
union
select '111.00$' from dual
union
select '1.2%' from dual
)
select regexp_replace(txt, '[^0-9.]', '')
from testdata;
Result of an SQL fiddle:

Query for regular expression - oracle DB

I am trying to get all the name which has special char and numbers in it. I want to know if there is a better way of writing this query. Thanks
select Name from TABLE1
WHERE NAME LIKE '%-%'
OR NAME LIKE '%$%'
OR NAME LIKE '%4%' etc
Try this:
SELECT Name FROM Test WHERE REGEXP_LIKE(Name,'(-|$|4)');
The | (OR or Alternation) operator allows the expression to return true if the value contains an '-' OR an '$' OR an '4' etc.
Use square brackets to define a character class. Also a full regex to anchor the pattern to the beginning of the string, any number of any characters before a character class consisting of a dash or a 4 or a dollar sign then any number of any characters anchored to the end of the string:
SQL> with tbl(name) as (
2 select 'efs' from dual
3 union
4 select 'abc-' from dual
5 union
6 select 'a4bd' from dual
7 union
8 select 'gh$dll' from dual
9 union
10 select 'xy5zzy' from dual
11 )
12 select name from tbl
13 where regexp_like(name, '^.*[-|$|4].*$');
NAME
------
a4bd
abc-
gh$dll
SQL>