Reg Ex - not allowing zero - regex

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

Related

Regex that matches even amount of character

Disclamer (after solved): this is my uni assignment thus I the answer could be simple. Hints are shown but my answer is hidden from here. Alternative answers could be found here but I take no responsibility with any plagiarism with direct answers posted here.
Hi I'm having troubles with the following exercise
Find regex that strictly represents the language:
b^(m+1), such that m>=0, m mod 2 = 1
The language breaks down to words:
{bb,bbbb,bbbbbb,bbbbbbbb,...}
I have tried the following:
b(bbb)?(bb)*
But this also accepts
{bb,bbb,bbbb,bbbbb,...}
Is there a way to write it such one bit of expression is depended on the other? ie: (bb)* cannot be chosen if (bbb)? is chosen at once, then repeat the decision but allow the vice versa.
Any help would be appreciated. Thanks
Update:-
You can use
^(?:bb)+$
Regex Demo
Initial heading of question was --> Regex that matches odd amount of character
You can try this
^b(?:(?:b{2})+)?$
Regex Demo
My guess is that, this might be closer,
^(?:bb){1,}$
and your set might look like,
bb
bbbb
bbbbbb
not sure though. If your set was correct, expression can likely be modified.
also, b would not probably be in the set, since m=0 does not pass the second requirement.
If you wish to explore/simplify/modify the expression, it's been
explained on the top right panel of
regex101.com. If you'd like, you
can also watch in this
link, how it would match
against some sample inputs.

Setting minimum and maximum value using 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.

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.

what Regex Expression do we need to use in this scenario

what Regex Expression do we need to use in this scenario
Iam looking to have Regular Expression where we need to validate the user that he cannot enter a value of "ZERO"
is this correct "regex":/^0$/,
Valid responses are:
1,2,3,4,5,6,7,8,9,10,20,
Invalid Responses are
0
No, and it is rather complicated and clumsy to write a regex saying "but not x". Try to avoid regexes in that case.
Often times, it's helpful to list a set of valid and invalid responses.
Valid:
joe
bob
zero
amy0
s0nny
0mar
Invalid:
0
If this is the case, then the easiest solution is to say username == "0", and skip the regular expression. If, rather, you need to make sure that the 0 can not be at the front of any string, then something like username[0] == '0' would work.
On the other hand, if you want to make sure that there are no 0 characters anywhere in the string, then I would use something like not username.contains('0').
I guess what I'm getting at is that this doesn't sound like a problem that demands a regular expression.
Try using this...
^(0.+|[^0].*)$
You can test it here...
http://regexpal.com