I try to do this
preg_replace("select (.*) from", "SELECT COUNT(*) FROM", $_pagi_sql)
but I get the following error
preg_replace(): Delimiter must not be alphanumeric or backslash in
What I can do?
You're not using a delimiter (/) for your patterns.
preg_replace("/select (.*) from/", "SELECT COUNT(*) FROM", $_pagi_sql
Related
I have a column with this sample data. I need to extract all substring that starts with "M6". Is there a way to do it with regexp_extract?
Data Column
HEY01230328_M6K21SG_UNO_NYC_241
M6EW2BJ_UNO_NYC_251
M6HW2WL_UNO_NYC_251
HEY08460329_NA_M6LAB3D_UNO_NYC_241
Desired Output
M6K21SG
M6EW2BJ
M6HW2WL
M6LAB3D
Try using:
SELECT colname FROM tableName WHERE REGEXP_EXTRACT(colname, ".*(M6[^_]*).*",1)
Regex used:
.*(M6[^_]*).*
Regex Demo
Explanation:
.* - matches 0+ occurrences of any character that is not a newline character
(M6[^_]*) - matches M6 followed by 0+ occurrences of any character that is not a _. So, after M6, it keeps on matching everything until it finds the next _. The parenthesis is used to store this sub-match in Group 1
.* - matches 0+ occurrences of any character that is not a newline character
I run the following statement to replace the characters of ".." to ".":
CREATE TABLE TableA AS
SELECT Column1,
REGEXP_REPLACE(Column2, "..", ".") AS NewColumn
FROM TableB;
The result of NewColumn became ".......", what's wrong with the REGEXP_REPLACE() function?
regexp_replace expects a regex pattern. . means any character in regex, so all pairs of characters are replaced with a fullstop because you specified .. as the regex pattern.
To prevent this, you can either escape the fullstop:
REGEXP_REPLACE(Column2, "\\.\\.", ".")
or use replace, which expects a string pattern:
REPLACE(Column2, "..", ".")
In addition to what #mck proposed, you can use quantifier for repeating patterns
REGEXP_REPLACE(Column2, "\\.{2}", ".")
Or if you want to replace 2 or more dots with single one:
REGEXP_REPLACE(Column2, "\\.{2,}", ".")
i tried to remove special characters and numeric number from string of sentence but it should ignore white spaces if there is a more than one it should replace with one
SQL developer,oracle 11g
select REGEXP_REPLACE ('Annapurna1# Poojari675&^','(\W|\d)','') from dual;
actually output is AnnapurnaPoojari but i need as Annapurna Poojari
You can be more explicit about the characters you want to keep:
select REGEXP_REPLACE('Annapurna1# Poojari675&^', '([^a-zA-Z ])', '')
from dual;
You can alternatively use [^[:alpha:] ]+ pattern to remove non-alphabetic characters and keep spaces :
select regexp_replace('Annapurna1# Poojari675&^','[^[:alpha:] ]','') as "Result String"
from dual;
Result String
-----------------
Annapurna Poojari
I have a string 'ABC.1.2.3'
I wish to replace the middle number with 1.
Input 'ABC.1.2.3'
Output 'ABC.1.1.3'
Input 'XYZ.2.2.1'
Output 'XYZ.2.1.1'
The is, replace the number after second occurrence of '.' with 1.
I know my pattern is wrong, the sql that I have at the moment is :
select REGEXP_REPLACE ('ABC.1.2.8', '(\.)', '.1.') from dual;
You can use capturing groups to refer to surrounding numbers in replacement string later:
select REGEXP_REPLACE ('ABC.1.2.8', '([0-9])\.[0-9]+\.([0-9])', '\1.1.\2') from dual;
You could use
^([^.]*\.[^.]*\.)\d+(.*)
See a demo on regex101.com.
This is:
^ # start of the string
([^.]*\.[^.]*\.) # capture anything including the second dot
\d+ # 1+ digits
(.*) # the rest of the string up to the end
This is replaced by
$11$2
I have some query to match a URL address, but the URL contains a special character '?' which I don't know how to escape.
the query is like:
select *
from table
where url = 'www.someaddress.com/**?something**=sth';
Any ideas?
You need to use an ESCAPE.
where url = 'www.someaddress.com/**?something**=sth';
would be
where url = 'www.someaddress.com/**#?something**=sth' ESCAPE '#';