regex for matching a word and numbers in same string - regex

I don't know regex and need to find the expressions to isolate strings that have the word "comp" plus any price (number)
any ideas?
249.00 | 259.00 | 279.00 | comp | 349.00 | //I need to return this as match
369.00 | 359.00 | 599.00 | //don't want to return this as match
299.00 | 499.00 | //don't want to return this as match
329.00 | //don't want to return this as match
comp | 269.00 | 269.00 | //I need to return this as match
179.00 | 239.00 | comp | //I need to return this as match
comp | //don't want to return this as match
89.00 | 89.00 | 89.00 | //no match
249.00 | //don't want to return this as match
comp | 249.00 | //I need to return this as matc
199.00 | comp | comp | //I need to return this as match
comp | comp | 99.00 | 99.00 | //I need to return this as match
comp | comp | comp | comp | comp | //I need to return this as match

Try
(\bcomp\b.+([0-9]*[.]?[0-9]+))|(([0-9]*[.]?[0-9]+).+\bcomp\b)
\bcomp\b for boundary word comp.
.+ for one to many characters.
[0-9]*[.]?[0-9]+ for float number.
| for or condition. number | comp or comp | number

Let try this
/\d?+comp\d?+/g
It will match all string "comp" with numbers. I think it right for you

Related

What is the maximum length of a regex expression?

In PostgreSQL, I want to exclude rows if the desc field contains any forbidden words.
items:
| id | desc |
|----|------------------|
| 1 | apple foo cat bar|
| 2 | foo bar |
| 3 | foocatbar |
| 4 | foo dog bar |
The forbidden words list is stored in another table, currently it has 400 words to check.
forbidden_word_table:
| word |
|---------|
| apple |
| boy |
| cat |
| dog |
| .... |
SQL query:
select id, desc
from items
where
desc !~* (select '\y(' || string_agg(word, '|') || ')\y' from forbidden_word_table)
I am checking if desc does not match the regex expression:
desc !~* '\y(apple|boy|cat|dog|.............)\y'
Results:
| id | desc |
|----|------------------|
| 2 | foo bar |
| 3 | foocatbar |
** 3rd is not excluded since cat is not a single word
My forbidden_word_table will likely grow with many rows, the above regex will become a very lengthy expression.
Do regex expressions have a maximum length limit (in bytes or characters)? I'm afraid of my regex matching approach will not work if forbidden_word_table keeps growing.
Seems, that Wiktor Stribiżew is right about "catastrophic backtracking".
I'd suggest to use ILIKE and ANY:
SELECT *
FROM items i
WHERE NOT i."desc" ILIKE ANY
(
SELECT '%' || word || '%'
FROM forbidden_word_table
);
db-fiddle

regex numbers in arithmetic expression

I want to capture all numbers in a string
for example:
+================+============+
| string | match |
+================+============+
| 5*-33 = 75.3 | 5|-33|75.3 |
+----------------+------------+
| s44+2=7 | 2|7 |
+----------------+------------+
| ii2*-5 = 46 | -5|46 |
+----------------+------------+
| -2*-2.1 = 0.1 | -2|-2.1|0.1|
+================+============+
i tried with following expression, but its not working with signed numbers.
\b([0-9]+(\.\d+)?)\b
Regexr
Don't forget the optional -. - is not a number, so you have to capture it separately.
\b(-?\d+(\.\d+)?)\b
Of course, this will have issues with valid expressions such as:
4-3
But that seems to be a different problem.

Remove certain letters in foma

I am trying to write a rule to remove the non-start [a | e | h | i | o | u | w | y] letters in a string. The rule should keep the first letter, but remove given letters in other locations.
For example,
vave -> vv
aeiou -> a
My code is as below:
?* [ a | e | h | i | o | u | w | y ]+:0 ?* [ a | e | h | i | o | u | w | y ]+:0;
However, when applying the rule on vaavaa, it returns
vaav
vava
vava
vav
vava
vava
vav
vvaa
vva
vva
vv
while vv is what I want.
Please share some advice. Thanks!
You may use this regex for search:
(?!^)[aehiouwy]+
and replace it by emptry string ""
RegEx Demo
RegEx Details:
(?!^): Lookahead to make sure it is not at start
[aehiouwy]+: Match one or more of these letters inside [...]
You can use a captured group and alternation
^(.)|[aehiouwy]+
replace by \1
Regex demo

REGEX: why '^([a-z] | a)$' does not match 'a'?

I never used regular expressions before and I was testing some examples.
What I don't understand is why the regular expression ^([a-z] | a)$ doesn't match the string 'a'.
As I understood [a-z] is equivalent to (a | b | c | ... | y | z), so
[a-z] | a must be equivalent to (a | b | c | ... | y | z) | a, that is the same
to say (a | b | c | ... | y | z) or [a-z].
For that reason a string str matches ^([a-z] | a)$ iff matches ^[a-z]$.
That's why I don't understand why that regular expression doesn't match string 'a' or 'e' for example.
PS: I was testing this in this page.
Spaces matter in regular expressions. Remove the spaces around the pipe (|) and it should work.

how to get proxy from string

I have string
76.125.85.66:16805 | 0.238 | Little Rock | AR | Unknown | United
States69.207.212.76:49233 | 0.274 | Sayre | PA | 18840 | United
States96.42.127.190:25480 | 0.292 | Sartell | MN | 56377 | United States
and heres how I get proxy from it
my code
Dim ip As String = "76.125.85.66:16805 | 0.238 | Little Rock | AR | Unknown | United States69.207.212.76:49233 | 0.274 | Sayre | PA | 18840 | United States96.42.127.190:25480 | 0.292 | Sartell | MN | 56377 | United States"
ip = Regex.Match(ip, "\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\:\d{2,5}\b", RegexOptions.Singleline).ToString
RichTextBox1.Text = ip
it only show first proxy 76.125.85.66:16805 but i want it show all
76.125.85.66:16805
69.207.212.76:49233
96.42.127.190:25480
Use the Regex.Matches() method instead and remove the beginning word boundary.
You could write it as follows:
For Each m As Match In Regex.Matches(ip, "(?:\d{1,3}\.){3}\d{1,3}:\d+")
Console.WriteLine(m.Value)
Next
Ideone Demo
use this pattern to return multi result for specific expression
public ArrayList HRefs(string incomingHtml)
{
ArrayList arrayList = new ArrayList();
string pattern = "href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))";
for (Match match = Regex.Match(incomingHtml, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled); match.Success; match = match.NextMatch())
{
string str = match.Groups[1].Value;
arrayList.Add(str);
}
return arrayList;
}