regexp in ON clause produces error - regex

I am running on a postgresql 8.2.15 database.
I am trying to run a query that does a join using regular expressions. My current query is:
SELECT *
FROM USCITY a join USCITY2 b
ON b.usps in regexp_matches(a.city, '.* (?= [^\\s]*, [^\\s]*$)');
this produces the error:
"ERROR: syntax error at or near "regexp_matches"
Position: 101"
I have checked to see that the regexp_matches function works using
SELECT regexp_matches(city, '.* (?= [^\\s]*, [^\\s]*$)')
FROM CITY2;
Thanks for your help.

Related

"invalid regular expression: parentheses () not balanced"

I'm getting the error invalid regular expression: parentheses () not balanced while executing a query. The error refers to this part:
substring(every_x1, '\m[0-9]*\.?[0-9]'),
substring(every_x2, '\m[0-9]*\(?|-|to|TO)'),
substring(every_x2, '\m[0-9]*\(?|time|TIME)')
I checked it in an online parentheses checker, and it's supposed to be okay. What am I doing wrong?
I dont know what is the exact pattern you are looking but if you were looking to find the actual "(" (parentheses) sign maybe you should escape it the second time also, somethin like in this example:
select substring(every_x2, '\m[0-9]*\(?|-|to|TO\)')::float as part_1
Try this regular expression '\m(\d+(?:\s*-\s*|\s*to\s*)\d+)\M').
select substring('123 12-56 xyz' from '\m(\d+(?:\s*-\s*|\s*to\s*)\d+)\M');
-- 12-56
select substring('123 12 to 56 xyz' from '\m(\d+(?:\s*-\s*|\s*to\s*)\d+)\M');
-- 12 to 56

Select the next line of the matched pattern in clob column using oracle regular expression

I have a clob column "details" in table xxx. I want to select the next line of the matched pattern using Regex.
Input Text (CLOB DATA) like below :( all placed in new line)
MODEL_DATA 1
TEST1:
NONE
TEST2:
NONE
INFO:
SERVICES,VALUED-YES
TYPE:
NONE
I tried to use INFO as pattern match string and retrieve the next line of the text . But could not able to do it by using Regular expression function . Please help me to resolve this
Output :
SERVICES,VALUES-YES
You can use the below to get the details
select replace(regexp_substr(details,'INFO:'||chr(10)||'.+'),'INFO:')
from your_table;
You can also try the below to be operation system independent
select replace(regexp_substr(details,'INFO:
('||chr(10)||'|'||chr(13)||chr(10)||').+'),'INFO:')
from your_table;

Postgres Query with Regex

I'm trying to create a regex to find (and then eventually replace) parts of strings in a PG DB. I'm using PSQL 9.0.4
I've tested my regex outside of PG and it works perfectly. However, it isn't playing well with PG. If anyone can help me understand what I'm doing wrong it would me much appreciated.
Regex:
{php}.*\n.*\n.*'mister_xx']\)\);.*\n} \n{\/php}
Postgres Query:
SELECT COUNT(*) FROM (SELECT * FROM "Table" WHERE "Column" ~ '{php}.*\n.*\n.*'mister_xx']\)\);.*\n} \n{\/php}') as x;
Postgres Response:
WARNING: nonstandard use of escape in a string literal
LINE 1: ...M (SELECT * FROM "Table" WHERE "Column" ~ '{php}.*\n...
^
HINT: Use the escape string syntax for escapes, e.g., E'\r\n'.
ERROR: syntax error at or near "mister_xx"
LINE 1: ..."Table" WHERE "Column" ~ '{php}.*\n.*\n.*'mister_x...
In SQL, quotes are delimited as two quotes, for example:
'Child''s play'
Applying this to your regex makes it work:
SELECT COUNT(*)
FROM "Table"
WHERE "Column" ~ '{php}.*\n.*\n.*''mister_xx'']\)\);.*\n} \n{\/php}' as x;
Note also how the redundant subquery .
You need to double escape the backslashes and add an E before the statement:
SELECT * FROM "Table" WHERE "Column" ~ E'{php}\n.\n.*''mister_xx'']\)\);.*\n} \n{\/php}'

Regex CHECK constraint not working with SQL server

im trying to reject all inputs not in the format "03 xxxx xxxx" so i created a table like
create table records
(
....
num varchar(255) NOT NULL,
...
CONSTRAINT num_check CHECK (num like '03 [0-9]{4} [0-9]{4}')
)
which should (i think?) accept for example "03 1234 1234". but if i try to add this via sql manager i get an error with the message:
"the INSERT statement conflicted with the CHECK constraint "num_check" "
at first i thought my Regex was off but ive tried it in a few other places and it accepts the example above.
any ideas?
like does not work with regular expressions, it has its own, much simpler wildcard patterns, which only support %, _ , [a-z], and [^a-z]. That's it. {4} would not works, just like most regex features.
You should be able to use:
like '03 [0-9][0-9][0-9][0-9] [0-9][0-9][0-9][0-9]'
Another option, a little less repetitive:
declare #digitChar nvarchar(12)
set #digitChar = '[0-9]'
Where clause:
like '03 ' + replicate(#digitChar,4) + ' ' + replicate(#digitChar,4)
Example: http://sqlfiddle.com/#!3/d41d8/3251

Using regex in WHERE in Postgres

I currently have the the following query:
select regexp_matches(name, 'foo') from table;
How can I rewrite this so that the regex is in the where like the following (not working):
select * from table where regexp_matches(name, 'foo');
Current error message is:
ERROR: argument of WHERE must be type boolean, not type text[]
SQL state: 42804
Character: 29
Write instead:
select * from table where name ~ 'foo'
The '~' operator produces a boolean result for whether the regex matches or not rather than extracting the matching subgroups.