How to set up REGEX that doesn't match anything? - regex

Just for curiosity.
Is it possible to create a regular expression that will not match any string, including an empty string?

Yes.
Here are a few examples.
.^
$.
(?!)
Naturally, there are an infinite number of such expressions.

This regex should never match anything (provided you do not use single-line or multi-line modifiers):
$x^

How about /^$x/. When I try it with ruby, it seems to work.

Related

Extract number + letter combination from string

I'm intrested in extracting c8127c6ea6a44c109b5e35ce61cd4b0096a9c6dc from a string that looks like this:
?t=c8127c6ea6a44c109b5e35ce61cd4b0096a9c6dc'
Here is my attempt at capturing the result in to a group.
?t=([a-e]\d+)'
Could anyone point me in the right direction, since this obviously isn't working?
http://regexr.com?383s6
You wanna put the \d in the [a-e] block and escape the ?:
\?t=([a-f\d]+)'
(and I assume you're looking for hexadecimal so it should be a-f?)
You can use this regex:
\?t=([a-e0-9]+)'
OR usig negation:
\?t=([^']+)'
In Ruby language you can try Rubular tool online to test your regular expressions.
\?t=(\w+)'
should do the match.

Perl Extended Regular Expressions - match with multiple question marks inside

I have got a weird thing to solve in perl using regular expressions.
Consider the strings -
abcdef000000123
blaDeF002500456
wefdEF120045423
All of these strings are matching with the below regular expression when I tried in C with pcre library support :
???[dD][eE][fF][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
But I'm unable to achieve the same in perl code. I'm getting some weird errors.
Please help with the piece of perl code with which these two things match.
Thanks in advance...
? is called quantifier that makes preceding pattern or group an optional match. Independently ? doesn't make any sense in regex and you are getting an error like: Quantifier follows nothing in regex.
Following regex should work for you in perl:
...[dD][eE][fF][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
OR even more concise regex:
.{3}[dD][eE][fF][0-9]{9}
Each dot means match any character.
PS: You probably are getting confused by shell's glob vs regex.
That looks more like a file system regex than a PCRE. In Perl, the ? is a quantifier, not a wild card. You may want to replace them with . to get the same results in anything Perl compatible.
I might use ...[dD][eE][fF][0-9]{9} or even replace the [0-9] with \d.
qr/[A-z]{3}def[0-9]{9}/i
should be the Perl Regex object used to validate the mentioned strings.
Regards

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 for just only numbers

I haven't used regular expressions soo much, so I'm having difficulty . I want regex that only validates that the field contains digits, but that does not care about how many.
It should approve 77 and 2377? But do not approve 77.43 or xyz777.
How can I get this using regular expression? Is this expression ^[0-9]+$ ok or not
It's OK. You can just use ^\d+$ for all it matters anyway.
Yes, this regex is perfectly valid and does what you think it does, although if your regex engine supports this you could use \d, whichs stands for [0-9].
A simpler regex would be to invert your match and check for non-digit numbers: \D.

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]$