How to use ? with a string in RegEx? - regex

Basically I want to check the following
www should not be in a string, however this obviously doesn't work as in the following regex any character is basically separated by a |. So w|w|w..
[^www]?
How can I solve this problem? Is there any easy possibility?

You don't need the optional operator ? here. You can use a Negative Lookahead assertion.
^(?!.*www).*$

[^www] is the same as [^w].
Using negative look-around can help you achieving that:
^((?!www).)*$
The above regex matches everything that doesn't contain www.

Use a negative lookahead:
^((?!www).)*$

Related

Regular expressions, negative lookahead anywhere in the string

Im sorry if this is asked and has an answer but I can't find it.
I know about regex lookarounds and negative lookahead.
Thing is that negative lookahead examines what comes right after current position in a string.
What I need is to find and discard matches if string contains words like "career(s)" and "specials" for example, but if it contains them anywhere in the string.
What would be the efficient way of doing that?
At the moment I'm using PCRE flavor but the more general regex is, the better.
Thank you.
You can use this regex:
^(?!.*(?:career\(s\)|specials)).*
Or if s is optional then use:
^(?!.*(?:career|special)s?).*
RegEx Demo

Regex to matches multiple string

need help to separate this string
value-filter-29[]=Test+1,Test+2,Test+2&+3&value-filter-43[]=Last+30+Days&value-filter-11[]=Testing+number
into this
value-filter-29[]=Test+1,Test+2,Test+2&+3
value-filter-43[]=Last+30+Days
value-filter-11[]=Testing+number
already tried with regex
(?:)(value-filter-\d+\[\]=.+?)(&|$)
and for the first match it will only take value-filter-29[]=Test+1,Test+2,Test+2 without &3
Is there any way to achieve this using regex?
You can use the following regex:
\&(?=value)
It will split at every & that is followed by value. You can fine-tune this look-ahead later. Looking at the current ouput, you might want to add -filter-: \&(?=value-filter-).
See demo
Output:
value-filter-29[]=Test+1,Test+2,Test+2&+3
value-filter-43[]=Last+30+Days
value-filter-11[]=Testing+number
You need to use a positive lookahead assertion.
value-filter-\d+\[\]=.+?(?=&value-filter-|$)
DEMO

capturing a repeating pattern with regex

I'm trying to match a pattern like this CODE-UH87H-98HSH-HB383-JWWB2U and I have the following regex pattern CODE\-[A-Z0-9]+\-[A-Z0-9]+\-[A-Z0-9]+\-[A-Z0-9]+ but is there a better way of doing this? I tried CODE(\-[A-Z0-9]+\-){4} and it didn't work
I tried CODE(\-[A-Z0-9]+\-){4} and it didn't work
That does require two dashes in succession. In full, it would be CODE\-[A-Z0-9]+\-\-[A-Z0-9]+\-\-[A-Z0-9]+\-\-[A-Z0-9]+\-. What you want is
CODE(\-[A-Z0-9]+){4}
You were almost there. CODE(\-[A-Z0-9]+){4} should work!
When the pattern between the dashes may contain any character, the following regex is even shorter:
CODE(-[^-]+){4}
Of course you may have to add \ for escaping before the dash depending on what regex engine you will use.

Oracle regex string not beginning with '40821'

I am trying to define a regex that matches string with numbers and it's not begining with 40821, so '40822433598347597' matches and '408211' not. So, I've tried
^(?!40821)\d+
Works perfectly in my regex editor, but still doesnt work in oracle. I know, it's very easy to use where not but my goal is to do it using only regex. Please, some pieces of advice, what am I doing somthing wrong?
According to this question, negative lookahead and lookbehind are not supported in Oracle.
One way would be to explicitly enumerate the possibilities using alternation. In your case it would be something like:
^([012356789]|4[123456789]|40[012345679]|408[013456789]|4082[023456789])
I think you try to use negative lookbehind:
(?<!a)b matches a "b" that is not preceded by an "a"
Source: http://www.regular-expressions.info/lookaround.html
That kind of Perl's sytax is not supported by Oracle.

Regex to check that a character in range doesn't repeat

I want to match against Strings such as AhKs & AdKs (i.e. two cards Ah = Ace of Hearts). I want to match two off-suit cards with a regex, what I currently have is "^[AKQJT2-9][hscd]{2}$", but this could match hands such as AhKh (suited) and AhAh. Is there a way to possibly use backreferences to say the second [hscd] cannot be the same as the firs (similarly for [AKQJT2-9])
Not perfectly elegant, but works:
^[AKQJT2-9]([hscd])[AKQJT2-9](?!\1)[hscd]$
Try this regular expression:
^[AKQJT2-9]([hscd])[AKQJT2-9](?!\1)[hscd]$
Here a negative look-ahead assertion (?!…) is used to disallow the fourth character to be the same as the second (match of first grouping).
But if the regular expression implementation does not support look-around assertions, you will probably need to expand it to this:
^[AKQJT2-9](h[AKQJT2-9][scd]|s[AKQJT2-9][hcd]|c[AKQJT2-9][hsd]|d[AKQJT2-9][hsc])$
a negative lookahead comes to the rescue
/^[AKQJT2-9]([hscd])[AKQJT2-9](?!\1)[hscd]$/
:( too late.
Yes. Use back-reference together with a negative look-ahead.
^([AKQJT2-9])([hscd])(?!\1)(?!.\2)[AKQJT2-9][hscd]$