ERROR: invalid regular expression: quantifier operand invalid - regex

I am trying to write a regular expression to get the "url" from the following text:
[![title](thumbnail_url?height=240&width=320)](url)
using the following postgresql query:
SELECT (SELECT m[1] FROM regexp_matches(text, '^\[\!\[^*\]\(^ *\)\]\(((^*))') AS r(m) LIMIT 1) FROM texts;
I am getting the following error while I execute the above query:
ERROR: invalid regular expression: quantifier operand invalid

If you want just the final "url", then this expression would do the trick:
.+\)\]\((\S+)\)
Throw away everything up to and including the literal )](, then capture everything until the literal ).
SELECT regexp_matches(text, '.+\)\]\((\S+)\)')[1] AS m
FROM texts
LIMIT 1;

Related

Regex in PostgreSQL

I'm ultimately trying to use the following regex expression.
SELECT *
into table
FROM table2
Where
(Description ~ '\bD\s*(&|AND|&|N|AMP|\*|\+)\s*B.*')
However this returns the following errors:
[XX000] ERROR: Invalid preceding regular expression prior to repetition operator. The error occured while parsing the regular expression fragment: 'P;|N|AMP|>>>HERE>>>|+)sB.'. Detail: ----------------------------------------------- error: Invalid preceding regular expression prior to repetition operator. The error occured while parsing the regular expression fragment: 'P;|N|AMP|>>>HERE>>>|+)sB.'. code: 8 ...
Any idea on the fix?
You should replace \b with \y (or \m) to fix the pattern, and you may put single chars inside a capturing group into a character class where you do not have to escape them, (&|\*|\+) -> [*+&]. Note you do not need .* at the end, unless you are matching (if you just check for a regex match with ~ you do not need it);
Use
'\yD\s*(AND|&|N|AMP|[*+&])\s*B'
See the online demo:
CREATE TABLE tb1
(website character varying)
;
INSERT INTO tb1
(website)
VALUES
('D AND B...'),
('ROCK''N''ROLL'),
('www.google.com'),
('More text here'),
('D N Brother')
;
SELECT * FROM tb1 WHERE website ~ '\yD\s*(AND|&|N|AMP|[*+&])\s*B';
Output

Regular expression for java

I have 2 regular expressions that I want to combine.
First Regex:
(?:for invalid user|for user|user|for|to account) (?:\"|')?(\\S+?)(?:\"|'|'s|,)?(?:\\.|\\s.*?)$
This regular expression captures the username from this string:
<37>May 14 10:02:10 imapd[7336]: [ID 210418 auth.notice] Login failed
user=k33360 auth=k33360 host=bas3-stlambert20-1176368555.dsl.bell.ca
[70.29.245.171]
Second Regex:
(?:\\w*\\:?\\s?\\[ID \\d+ \\w+.\\w+\\]\\s*(\\w*)?.*?)
This regular expression captures the username from this string:
<33>Jul 16 07:55:44 sudo: [ID 702911 auth.alert] do0905 : 1
incorrect password attempt ; TTY=pts/2 ; PWD=/home/do0905/bin ;
USER=root ; COMMAND=/usr/bin/su -
Now I want to combine both of these regular expression with the OR condition, so that it is true for both the string.
I have tried using this regular expression, but it doesn't work:
(?:for invalid user|for user|user|for|to account) (?:\"|')?(\\S+?)(?:\"|'|'s|,)?(?:\\.|\\s.*?)$ | ((?:\\w*\\:?\\s?\\[ID \\d+ \\w+.\\w+\\]\\s*(\\w*)?.*?))
How can I combine these two regular expressions using the OR condition?
Try to combine it like this:
Pattern.compile("(?i)(?:for invalid user|for user|user|for|to account)(?:\"|')?(\\S+?)(?:\"|'|'s|,)?(?:\\.|\\s.*?)|((?:\\w*:?\\s?\\[ID \\d+ \\w+.\\w+]\\s*(\\w*)?.*?))");

Why do I get the error "Unmatched ( in regex" when trying to match something with Net::Telnet?

I'm attempting to match the string
Save settings:([Y]/[N]):
that the telnet client returns after issuing
print("exit")
(I'm using Net::Telnet.)
I have tried several regexes, including this one:
waitfor(“/^\s+ Save settings:([Y]/[N]):\s$/”
but I continue to receive the error:
bad match operator: Unmatched ( in regex; marked by <-- HERE in m/\s+Save settings?( <-- HERE [Y]/ <$data> line 1. at printer_config_test.pl line 36
How can I fix this?
You are providing the following regex match operator:
/^s+ Save settings:([Y]/ (followed by junk)
If you want to match the following string:
Save settings:([Y]/[N]):
You want the following regex pattern:
^\s*Save settings:\(\[Y\]/\[N\]\):
But waitfor wants a string containing a match operator. The following is the desired match operator:
/^\s*Save settings:\(\[Y\]\/\[N\]\):/
And the following is a string literal to that creates that string:
"/^\\s*Save settings:\\(\\[Y\\]\\/\\[N\\]\\):/"
So:
waitfor("/^\\s*Save settings:\\(\\[Y\\]\\/\\[N\\]\\):/")

Regex and textmatching issue

I am doing some basic text matching in Postgres 9.3.5.0.
Here is my code so far:
Select text from eightks
WHERE other_events = true and
keywordRegexs = [\y(director and member \s+ and resigned)\y/ix];
I am getting the following errors
psql:test3.sql:3: invalid command \y(director
psql:test3.sql:5: ERROR: syntax error at or near "["
LINE 3: keywordRegexs = [
I am trying to find documents which contain those exact phrases.
The regular expression match operator in Postgres is ~.
The case insensitive variant is ~*.
Branches are enclosed in ().
SELECT text
FROM eightks
WHERE other_events = true
AND keywordregexs ~* '(\y(director | member \s+ |resigned)\y)';
The meaning of "those exact phrases" is not clear in the question.
Details in the manual.

NSRegularExpression Creation/Escaping Error

I want to create the following regular expression with NSRegularExpression:
+(,|.|\n|\s)
One or more occurrences of any of these: comma, full stop (period), new line, whitespace.
I've attempted to create the NSRegularExpression as follows:
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:#"+(,|.|\\n|\\s)" options:NSRegularExpressionCaseInsensitive error:&error];
The important bit being:
regularExpressionWithPattern:#"+(,|.|\\n|\\s)"
From the class reference, there is this information about escaping/quoting:
Character Expression: \
Description: Quotes the following character. Characters that must be quoted to be treated as literals are * ? + [ ( ) { } ^ $ | \ . /
So, I want the . to be treated as a literal. I've tried the following:
+(,|\.|\\n|\\s)
+(,|\\.|\\n|\\s)
None of the above work; all result in nil for regex with NSCocoaErrorDomain and NSInvalidValue in error.
Can anyone tell me how to create the regular expression that I want?
Thanks.
You might have the quantifier + in the wrong place. In most implementations of Regex the quantifier is applied to a group/character by putting it afterwards, like so:
(,|\\.|\\n|\\s)+