Query for regular expression - oracle DB - regex

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>

Related

check if string has the format : 2 lettes au max following with numbers with regexp_like oracle

Could you please tell me the expression regexp_like to check if a string has the format which begins with one or two letters max followed by numbers. For example, like 'A25', 'AB567'.
Thanks
How about
SQL> with test (col) as
2 (select 'A25' from dual union all
3 select 'AB567' from dual union all
4 select 'A12A532A' from dual union all
5 select 'ABC123' from dual union all
6 select '12XYZ34' from dual
7 )
8 select col,
9 case when regexp_like(col, '^[[:alpha:]]{1,2}[[:digit:]]+$') then 'OK'
10 else 'Wrong'
11 end result
12 from test;
COL RESULT
---------- ----------
A25 OK
AB567 OK
A12A532A Wrong
ABC123 Wrong
12XYZ34 Wrong
SQL>
The regular expression would be ^[A-Z]{1,2}[0-9]+$.
Working demo on db<>fiddle here
This match a string that contain only numbers and letters, that start with a letter, when there is never more than 2 letters in a row.
^(([A-Za-z]){1,2}([0-9])+)*\2{0,2}$

matching patterns for regexp_like in Oracle to include a group of character conditionally

I tried to look up for a good documentation for matching pattern for use of regexp_like in Oracle. I have found some and followed their instructions but looks like I have missed something or the instruction is not comprehensive.
Let's look at this example:
SELECT * FROM
(
SELECT 'ABC' T FROM DUAL
UNION
SELECT 'WZY' T FROM DUAL
UNION
SELECT 'WZY_' T FROM DUAL
UNION
SELECT 'WZYEFG' T FROM DUAL
UNION
SELECT 'WZY_EFG' T FROM DUAL
) C
WHERE regexp_like(T, '(^WZY)+[_]{0,1}+[A-Z]{0,6}')
What I expect to receive are WZY and WZY_EFG. But what I got was:
What I would like to have is the "_" could be present or not but if there are character after the first group, it is mandatory that it be present only once.
Is there a clean way to do this?
Use a subexpression grouping to make sure the _ character appears only with Capitalized Alphabetical Characters
Yes, your pattern does not address the conditional logic you need (only see the _ when capitalized alphabetical characters follow).
Placing the _ character in with a capitalized alphabetical character list into a subexpression grouping forces this logic.
Finally, placing the end of line anchor addresses the zero match scenarios.
SCOTT#DB>SELECT
2 *
3 FROM
4 (
5 SELECT 'ABC' t FROM dual
6 UNION ALL
7 SELECT 'WZY' t FROM dual
8 UNION ALL
9 SELECT 'WZY_' t FROM dual
10 UNION ALL
11 SELECT 'WZYEFG' t FROM dual
12 UNION ALL
13 SELECT 'WZY_EFG' t FROM dual
14 ) c
15 WHERE
16 REGEXP_LIKE ( t, '^(WZY)+([_][A-Z]{1,6}){0,1}$' );
T
__________
WZY
WZY_EFG

To remove leading alpha characters from string

I have to Ignore the leading zeros, leading/trailing spaces, leading alpha characters from a string. Please help what regexp can be used.
For example:
the string is abc123abc , then it needs to return 123abc.
Presently i used
REGEXP_SUBSTR('abc123abc','([1-9]+[0-9]*)( *)$')
but it returns null for me.
Something like this?
SQL> with test (col) as
2 (select 'abc123abc' from dual union all
3 select ' 1234ddc' from dual union all
4 select '0abcd' from dual union all
5 select '18858 ' from dual union all
6 select 'ab123ab45' from dual
7 )
8 select col, trim(regexp_replace(col, '^[[:alpha:]]+| |0')) result
9 from test;
COL RESULT
--------- ---------
abc123abc 123abc
1234ddc 1234ddc
0abcd abcd
18858 18858
ab123ab45 123ab45
SQL>
Vaish,
This is how the Regex should be.
What this will do, it will remove any leading spaces, leading zeros.
Example of query:
SELECT REGEXP_SUBSTR('abc123abc','[1-9]+.*') from dual;
You can see some examples I have tried here, plus you can test some more here too.
https://regex101.com/r/zfohRB/1
Regex: '[1-9]+.*'
Explanation:
[1-9] - This will look for the number to start. Excluding 0.
+ - Quantifier + denotes 1 or more.
. - Means anything after that.
* - Means 0 or more. (You can replace this with + if you think that you need at least something after numbers.)
Good Luck

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: