Regex Expression Query - regex

I have data as
CVE-2011-0573,
CVE-2011-0606,
(CVE-2011-0565)
CVE-2011-0598,
CVE-2011-0593.
((CVE-2011-0593.)
Could you please help me writing RegEx so I only get ABC-####-#### ? The last four digit may vary, so it can have e.g. three or five digits, but most likely not more than ten. Also the expresion may contain some spaces in the end, so those need to be removed as well.

You can use this regex for matching:
[A-Z]{3}-[0-9]{4}-[0-9]{3,10}
If you have multiline flag available then use
/^[A-Z]{3}-[0-9]{4}-[0-9]{3,10}$/mg

Related

Regex for *either or both* of last two characters are not digits?

I can't figure out the proper regular expression for this... Most of my data ends with digits as the last two characters. A subset ends with where either one or both of the last two are non-digits. So xyz99 is normal and I'm able to find those records with "*[0-9][0-9]$". If I change that to "*[^0-9][^0-9]$" then I get records where both are non-digits.
I don't know regex well enough to match all of the following with a single regex: xy9z, xyz9, xyzw, but not matching xyz99.
I prefer a single regex, but (already know how to and) can work-around with multiple.
Thanks for any help.
[^\d]$|[^\d].$
should do the trick
https://regex101.com/r/PsZxLj/2
It matches anything that doesn't end in a digit OR anything where the 2nd to last character isn't a digit. Lots of ways to do this, but pick one that is easy for you to read and maintain. :) Good luck!
Something like this should do it:
(?:\d[^\d]|[^\d].)$
You could use a negative lookahead (?!...).
(?![0-9]{2}).{2}$
This will first make sure that [0-9]{2} (2 digits) does not match. Then proceeds to match the remaining regex, which matches any 2 characters .{2} followed by the end of the string $.
Regexper
Thank you all for the quick and helpful responses. A couple of the references above that start with "(?" are beyond what I understand so far. But the "or" operator is what I was missing. Here is what I ended up using (and it worked): select * from mytable where regexp_like( myfield, '.*([^0-9]|[^0-9][0-9])$' );

Regex: split number into optional first group of up to three then last group of up to three

I have two 1-6 digit numbers separated by a slash. I want these split up into groups of at most 3 digits, taking from the right.
For example:
0/1 -> [,0,,1]
1234/3 -> [1,234,,3]
12345/1234 -> [12,345,1,234]
123456/789123 -> [123,456,789,123]
I need to use a regular expression to do this because I want to do this for a location in NGINX. It's possible to do this with application logic but that is not the question due to performance.
Similar question which solves part of this was here using a negative lookahead: Regular expression to match last number in a string
What regex can achieve this split?
UPDATE:
This regex comes close to what I want (https://regex101.com/r/bQtNdK/3):
(?<prefix1>\d{0,3}?)(?<threes1>\d{0,3})\/(?<prefix2>\d{0,3}?)(?=\d)(?<threes2>\d{0,3})
It fails matching if the second number behind the slash is more than 3 digits long.
UPDATE2:
Now this regex works for most combinations (https://regex101.com/r/bQtNdK/5):
(?<prefix1>\d{0,3}?)(?<threes1>\d{1,3})\/(?<prefix2>\d{0,3})(?<threes2>\d{3})
I don't understand why this starts to fail if I use the same regex for prefix2/threes2 like prefix1/threes1 (i.e. make prefix2 also lazy). Any ideas how to solve this? So close...
I don't know that it's possible without the ability for the regex engine to remember all intermediate matches of a match group that matched an arbitrary number of times (.NET can do this, not sure what others). PCRE will apparently only remember the 'last' match for each group, other wise you could use something like this : (?<prefix1>\d{0,2})(?:(?<threes1>\d{3})*)\/(?<prefix2>\d{0,2})(?<threes2>\d{3})*\s
This regex seems to be correct now (regex101):
(?<prefix1>\d{0,3}?)(?<suffix1>\d{1,3})\/(?<prefix2>\d{0,3}?)(?<suffix2>\d{1,3})\/

Pattern matching software builds

I need to check if a XML document contains some software build numbers
normally they're like ######, where:
First two characters are numbers
Third character is a letter in uppercase
last three characters are numbers
Example: 10B329 or 11A465.
But there could be some exceptions, like 8L1 or 11B465a. (if there's another character after the sixth, it's always a letter in lowercase).
I think they're always with a minimum length of 3 characters and a maximum length of 7 characters.
So what could be the best pattern to match? I tried this but it doesn't work since it takes also words...
Dim BuildPattern As String = "<key>[0-9A-Z]*</key>"
Try this Regex: \d{2}\w\d{3}
You can see a live demo here.
You can add the <key></key> tags too: <key>(\d{2}\w\d{3})<\/key>. This way, your match will be in group 1 of the match. Changed demo.
Note that you should rather use XML parser for this as it's safer and more accurate than working with regex on XML files.
EDIT: Can't help you with the non-standard length though, my knowledge of regexes is still too low. Perhaps you really should try XML parser instead?

Regex Lookahead character restrictions?

I am trying to learn some things about Regex. I am starting off by trying to hide some matches for a nine digit number, such as a SSN, but let through all nine digit numbers that have the word "order" or "routing number" but it seems that only strings that have the same length will work. Is there any way around this without creating multiple lines? Thanks!
(?<!(Order:\s|Routing\snumber:\s))
(?!000|666)([0-6]\d\d|7[01256]\d|73[0123]|77[012])
([-]?)
([1-9]{2})
\3
([1-9]{4})
(?!([\w&/%"-]))
For blocking out SSNs, this one seems to work
^(?!000)(?!666)(?!9)\d{3}([- ]?)(?!00)\d{2}\1(?!0000)\d{4}$
but I want it to not block out any 9 digit numbers that have the words "order" or "routing number" in front of them.
Many regular expression engines require lookbehind to be of fixed length, and will refuse to execute a variable-length lookbehind; if this is the case for yours, you should see a warning. If you're not seeing a warning, chances are the problem is that your regexp simply doesn't wok the way you think it does.
However, it is usually possible with lookbehinds to simply match the text you would prefer to count as a lookbehind, then discard/ignore it when you're inspecting the captures or match object.

Regex pattern for a password

I need a regex pattern that validates a password format.
The rules are:
Minimum 8 chars in total
at least two letters
at least two digits or symbols
I came up with the following:
/((?=.*[0-9\#\&#\$\?\%!\|(){}[]])(?=.*[a-zA-Z]).{8,})/
It will look if both occure once, but I need it toch validate if they occur at least twice.
If I add the {2,} like this:
/((?=.*[0-9\#\&#\$\?\%!\|(){}[]]{2,})(?=.*[a-zA-Z]{2,}).{8,})/
Then the following doesnt work for example: a1a1a1a1a1
Can anybody help me?
This is how you do it, using positive lookaheads: http://regex101.com/r/uW0yI4
/^(?=.*[a-z].*[a-z])(?=.*[!"#...\d].*[!"#...\d]).{8,}$/gmi
Just replace !"#... with all the symbols you want to match.
Note: the multiline flag might be unnecessary for your applications.
This should give you what you're after:
^((?=(.*[\d0-9\#\&#\$\?\%!\|(){}[\]]){2,})(?=(.*[a-zA-Z]){2,}).{8,})$