Setting minimum and maximum value using regex - regex

I want a textbox to have a value between 0 and 24. However the below pattern does not seem to be working.
Regex regex = new Regex("^([0-9]|1[0-9]|2[0-4])$");

I'm not sure what WPF is, but this expression might likely validate 0-24:
^[0-9]$|^[1][0-9]$|^[2][0-4]$
In this demo, the expression is explained, if you might be interested.

Related

How to correct regular expression

I have something like this https://regexr.com/3ja39 and the question is: How should I change regexp to get value beetween lot and ohm but without lot and ohm I want to get only value which is beetewm
Use parenthesis in Regex to capture specific text between other characters, if this is always going to be a numeric between lot- and -ohm you could do something like:
.?lot-(\d.)-ohm.*?
I like this site, it has a great cheat sheet on the regex characters and what they do Regex Cheatsheet!
Javascript Regex

REGEX number not in a list failing with a long list

I have a list of the following numbers and want a Regular expression that matches when a number is not in the list.
0,1,2,3,4,9,11,12,13,14,15,16,18,19,250
I have written the following REGEX statement.
^(?!.*(0|1|2|3|4|9|11|12|13|14|15|16|18|19|250)).*$
The problem is that it correctly gives a match for 5,6,7,8 etc but not for 17 or 251 for example.
I have been testing this on the online REGEX simulators.
This should resolve your issue..
^(?!\D*(0|1|2|3|4|9|11|12|13|14|15|16|18|19|250)\b).*$
In your earlier regex you were basically saying eliminate all numbers that start with 0/1/2/3/4/9!
So your original regex would actually match 54/623/71/88 but not the others. Also the 11-19 and 250 in the list were rendered useless.
Although as others have I would also recommend you to not use regex for this, as I believe it is an overkill and a maintenance nightmare!
Also an extra note "Variable length look arounds are very inefficient too" vs regular checks.
I would do \b\d+\b to get each number in the string and check if they are in your list. It would be way faster.
You can use the discard technique by matching what you do not want and capturing what you really want.
You can use a regex like this:
\b(?:[0-49]|1[1-689]|250)\b|(\d+)
Here you can check a working demo where in blue you have the matches (what you don't want) and in green the content you want. Then you have to grab the content from the capturing group
Working demo
Not sure what regex engine you are using, but here I created a sample using java:
https://ideone.com/B7kLe0

Regular Expression to find CVE Matches

I am pretty new to the concept of regex and so I am hoping an expert user can help me craft the right expression to find all the matches in a string. I have a string that represents a lot of support information in it for vulnerabilities data. In that string are a series of CVE references in the format: CVE-2015-4000. Can anyone provide me a sample regex on finding all occurrences of that ? obviously, the numeric part of that changes throughout the string...
Generally you should always include your previous efforts in your question, what exactly you expect to match, etc. But since I am aware of the format and this is an easy one...
CVE-\d{4}-\d{4,7}
This matches first CVE- then a 4-digit number for the year identifier and then a 4 to 7 digit number to identify the vulnerability as per the new standard.
See this in action here.
If you need an exact match without any syntax or logic violations, you can try this:
^(CVE-(1999|2\d{3})-(0\d{2}[1-9]|[1-9]\d{3,}))$
You can run this against the test data supplied by MITRE here to test your code or test it online here.
I will add my two cents to the accepted answer. Incase we want to detect case insensitive "CVE" we can following regex
r'(?i)\bcve\-\d{4}-\d{4,7}'

problems with a regular expression

I'm trying to get a correct regular expression for this problem:
I get a result from a webservice as a string that looks like this
tag: 54e16e66 (Mifare card standard)
I need to extract the 54e16e66 part (which is in hexa) only, regardless of what is before and after that part, i've tried several patterns, but I am not confident enough with regex and I can't seem to find a pattern that works.
Edit
"tag :" is aways present.
the hexa part I want to extract has variable length
what is after is a user comment, so it could be anything
Could someone help ? thanks in advance !
Michael
Assuming your string there is a good template, and assuming a posix regular expression parser,
/^tag: ([0-9a-f]{8})/
should work. If the length is variable, you would just change the {8} to {m,n}, where m and n are the minimum and maximum possible number of characters you can expect, respectively.

Reg Ex - not allowing zero

Excuse my ignorance. My knowledge of regular expressions is extremely limited.
I have the following regular expression on a regular expression validator.
^(\d{1,3},?(\d{3},?){0,2}\d{3}|\d{1,3})$
It currently allows zero. I need to update it so that it does not allow a value of 0.
[Edit: Just to clarify, I would like it to exclude a value of zero rather than a singular "0" - thanks]
Any help would be greatly appreciated.
Thanks
zaps
You may be looking for something like this:
^[1-9]\d{0,2}(\d*|(,\d{3})*)$
0 is allowed by the second part of your regex. Change it to:
^(\d{1,3},?(\d{3},?){0,2}\d{3}|[1-9]\d{0,2})$
It makes sure that the first digit is non zero, when the total number of digits are less than or equal to three.
The regex still allows patterns like 000,000,000 and 000,123 To fix that you can change the first part of the regex to:
^([1-9]\d{0,2},?(\d{3},?){0,2}\d{3}|[1-9]\d{0,2})$
Or rewrite it as
^[1-9]\d{0,2}(,?\d{3}){0,3}$
This still allows 123,456789 and 123456,789. Let us change it to:
^[1-9]\d{0,2}(?:(,\d{3}){0,3}|(\d{3}){0,3})$
This will allow 123,456,789 and 123456789 but not 123,456789 or 123456,789