Extract substring that has special character - Oracle - sql

In my column, there is a string that contains a word which starts with character '=' How can I extract those words? I found REGEXP_SUBSTR but I couldn't find out particular regular expression to do this? I appreciate any help. Thanks.
EDIT :
I have such a string :
"What a =lovely day!"
I want to get "=lovely"

You can use regexp_substr for this:
select regexp_substr(col, '=\S+')
from your_table;
=\S+:
= - match literal =
\S+ - match one or more non space characters

Try
REGEXP_SUBSTR('What a =lovely day!', '=\w+')
or
REGEXP_SUBSTR('What a =lovely day!', '=\S+')
depending on your needs.

Also if you want to match based on a list of special characters, use something like below.
In this example, you can match = and #. You can add more special character if you like.
Also if you want just = to be returned in case rest word is missing after that, then use \S*. Else use \S+
For strings which dont have this format, you will get null.
select regexp_substr(col1,'[=#]\S*') from
(select 'what a =lovely day' as col1 from dual union all
select 'some other #word as' from dual union all
select 'a normal string' from dual)

Related

replace all occurrences of a sub string between 2 charcters using sql

Input string: ["1189-13627273","89-13706681","118-13708388"]
Expected Output: ["14013627273","14013706681","14013708388"]
What I am trying to achieve is to replace any numbers till the '-' for each item with hard coded text like '140'
SELECT replace(value_to_replace, '-', '140')
FROM (
VALUES ('1189-13627273-77'), ('89-13706681'), ('118-13708388')
) t(value_to_replace);
check this
I found the right way to achieve that using the below regular expression.
SELECT REGEXP_REPLACE (string_to_change, '\\"[0-9]+\\-', '140')
You don't need a regexp for this, it's as easy as concatenation of 140 and the substring from - (or the second part when you split by -)
select '140'||substring('89-13706681' from position('-' in '89-13706681')+1 for 1000)
select '140'||split_part('89-13706681','-',2)
also, it's important to consider if you might have instances that don't contain - and what would be the output in this case
Use regexp_replace(text,text,text) function to do so giving the pattern to match and replacement string.
First argument is the value to be replaced, second is the POSIX regular expression and third is a replacement text.
Example
SELECT regexp_replace('1189-13627273', '.*-', '140');
Output: 14013627273
Sample data set query
SELECT regexp_replace(value_to_replace, '.*-', '140')
FROM (
VALUES ('1189-13627273'), ('89-13706681'), ('118-13708388')
) t(value_to_replace);
Caution! Pattern .*- will replace every character until it finds last occurence of - with text 140.

Extract Specific Set of data from a String in Oracle

I have the string '1_A_B_C_D_E_1_2_3_4_5' and I am trying to extract the data 'A_B_C_D_E'. I am trying to remove the _1_2_3_4_5 & the 1_ portion from the string. Which is essentially the numeric portion in the string. any special characters after the last alphabet must also be removed. In this example the _ after the character E must also not be present.
and the Query I am trying is as below
SELECT
REGEXP_SUBSTR('1_A_B_C_D_E_1_2_3_4_5','[^0-9]+',1,1)
from dual
The Data I get from the above query is as below: -
_A_B_C_D_E_
I am trying to figure a way to remove the underscore towards the end. Any other way to approach this?
Assuming the "letters" come first and then the "digits", you could do something like this:
select regexp_substr('A_B_C_D_E_1_2_3_4_5','.*[A-Z]') from dual;
This will pull all the characters from the beginning of the string, up to the last upper-case letter in the string (.* is greedy, it will extend as far as possible while still allowing for one more upper-case letter to complete the match).
I have the string '1_A_B_C_D_E_1_2_3_4_5' and I am trying to extract the data 'A_B_C_D_E'
Use REGEXP_REPLACE:
SQL> SELECT trim(BOTH '_' FROM
2 (REGEXP_SUBSTR('1_A_B_C_D_E_1_2_3_4_5','[0-9]+', ''))) str
3 FROM dual;
STR
---------
A_B_C_D_E
How it works:
REGEXP_REPLACE will replace all numeric occurrences '[0-9]+' from the string. Alternatively, you could also use POSIX character class '[^[:digit:]]+'
TRIM BOTH '_' will remove any leading and lagging _ from the string.
Also using REGEXP_SUBSTR:
SELECT trim(BOTH '_' FROM
(REGEXP_SUBSTR('1_A_B_C_D_E_1_2_3_4_5','[^0-9]+'))) str
FROM dual;
STR
---------
A_B_C_D_E

Oracle SQL : Pattern matching using LIKE operator to discover two word patterns

I am trying to figure out how to query a column for a two word pattern like "allergic response" or "allergy response". I tried the below
LIKE '% allerg% response %' but this matches strings like "Allergic
reaction on 1/1/2010.... no response yet...."
Tried word boundary LIKE '% \ballerg%\b response %'. This
expression seems to be wrong. never worked
Basically I am looking to match consecutive words in which one of the words can take many values. I really dont want to list out all possible values in a LIKE '..' OR LIKE '..' .
Can someone give me some tip on how to approach this problem?
You can use the or operator | and group the alternatives inside parenthesis
For your example, you'll need the following regex:
allerg(ic|y) response
So your query becomes something like:
... WHERE REGEXP_LIKE (col_name, 'allerg(ic|y) response', 'i');
Remove the 'i' parameter for case sensitive matching
And as a side note, oracle curiously has no word boundary regex operator !! => source
You can use the regular expression \S* (zero-or-more non-white-space characters) or \w* (zero-or-more word characters) to match any ending to a word:
WITH phrases ( phrase ) AS (
SELECT 'Allergic Response' FROM DUAL UNION ALL
SELECT 'Allergy response' FROM DUAL UNION ALL
SELECT 'Allergy gives a response to ...' FROM DUAL
)
SELECT phrase
FROM phrases
WHERE REGEXP_LIKE( phrase, 'Allerg\S* response', 'i' );
Outputs:
PHRASE
----------------------
Allergic Response
Allergy response

RegEx: Repeated identical vowels in a string - Oracle SQL

I need to only display those strings (name of manufacturers) that contain 2 or more identical vowels in Oracle11g. I am using a RegEx to find this.
SELECT manuf_name "Manufacturer", REGEXP_LIKE(manuf_name,'([aeiou])\2') Counter FROM manufacturer;
For example:
The RegEx accepts
OtterBox
Abca
abcA
The RegEx rejects
Samsung
Apple
I am not sure how to proceed ahead.
I think you want something like this:
WITH mydata AS (
SELECT 'OtterBox' AS manuf_name FROM dual
UNION ALL
SELECT 'Apple' FROM dual
UNION ALL
SELECT 'Samsung' FROM dual
)
SELECT * FROM mydata
WHERE REGEXP_LIKE(manuf_name, '([aeiou]).*\1', 'i');
I am not sure why you used \2 as a backreference instead of \1 -- \2 doesn't refer to anything in this regex. Also, note the wildcard and quantifier .* to indicate that there can be any number of any character between the first occurrence of the vowel and the second. Third, note the 'i' parameter to indicate a case-insensitive search (which I think is what you want since you say that the regex should match "OtterBox").
SQL Fiddle here.
David yours wasn't quite working for me. What about this?
\w*([aeiou])\w*\1+\w*
https://regex101.com/r/eE3iC2/3
EDIT: updated one per suggestions:
.*([aeiou]).*\1.*
https://regex101.com/r/eE3iC2/5

Regular Expression in Oracle with REGEXP_SUBSTR

I want to get the email address part of a string.
For example if the string is
"your name(your#name.com)" aaa#bbb.com
then I want to get only
aaaa#bbb.com
basically if I can remove the string within
""
then it does the trick. I am using below regular expression with REGEXP_SUBSTR
REGEXP_SUBSTR('"your name(abc#dd.com)" aaa#bbb.com',
'([a-zA-Z0-9_.\-])+#(([a-zA-Z0-9-])+[.])+([a-zA-Z0-9]{2,4})+')
kindly help.
You can simply indicate that the match must occur at the end of the string, using $ anchor.
with t1(col) as(
select '"your name(your#name.com)" aaa#bbb.com' from dual
)
select regexp_substr(col, '[[:alnum:]._%-]+#[[:alnum:]._%-]+\.com$') as res
from t1
Result:
RES
-----------
aaa#bbb.com
You probably need something more along the lines of:
REGEXP_SUBSTR('"your name(abc#dd.com)" aaa#bbb.com','[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}')
Things like [.] doesnt really make sense, dot matches any character and the square brackets is a kind of "OR" statement where any character inside can go in that place, but in your case you actually want to match the literal dot so you need to escape that \. not sure how oracle handles the escapes, you might need to double escape them.
SELECT REGEXP_SUBSTR(email, '[A-Za-z0-9\_\-\.]+#\w+\.\w+', 1, 2) AS cleaned_email
FROM
(
SELECT '"your name(your#name.com)" aaa#bbb.com' AS email FROM DUAL UNION ALL
SELECT '"your name(your.name#name.com)" aaa#bbb.com' AS email FROM DUAL
)
;