Zero validation Regular Expression - regex

I have a small test to match using regex.
Match to true if and only if user has entered "0000" or 000 or 00 or 0.
0001 or 0011, 1000, 0111, 1111 should return false.
Here is what i have tried : /^0*([0]{1,4})/ but it didn't work.

This should work just fine:
/^0{1,4}$/

Related

Regular expression for hexadecimal string

I am trying to create a regex that detect if a string of hexadecimal is only a combination of 00 , 06, 03 and space.
The closest i've found so far is ^(00|06|03)$ but it's still giving me false for 0300
0300 will match
0600 0300 match
0612 0300 no match
3030 no match
^(00|06|03)$ will only match '00' or '06' or '03'. If you're expecting this combination to repeat, you need to add +.
Try ^(00|06|03| )+$
I've included the space in there as well. This will match your scenarios.
Close, but you need closure and some safe-guarding:
let v = /^((00|06|03){2}\s)*(00|06|03){2}$/;
[
'0300',
'0600 0300',
'0612 0300',
'3030',
'0303 0000',
'0630 0300',
'8790 0060',
'03 0000',
'0003 0006 0000 0000 0303 0606 0600 0306 0000',
'0606 0603 0303'
].map(s => console.log("%s : %s", s, v.test(s)));

Regex Visa + Mastercard with Spaces need Add Amex

I have this Regex to validate Visa and Master card with spaces, it works perfect.
^(?:4\d{3}|5[1-5]\d{2}|6011|3[47]\d{2})([- ]?)\d{4}\1\d{4}\1\d{4}$
Visa
4111111111111111 = true
4111 1111 1111 1111 = true
Master
5500000000000004 = true
5500 0000 0000 0004 = true
American Express
340000000000009 = false
3400 0000 0000 009 = false
I need add Amex to the regex, I need the last two true, can anyone help me please?
Thank you!
(assuming 4111111111111111 = false is a typo)
You can use the following regex :
^(?:4\d{3}|5[1-5]\d{2}|6011|3[47]\d{2})([-\s]?)\d{4}\1\d{4}\1\d{3,4}$
see demo
I do not know the background how you use this regex but i would do this in two steps:
Step 1:
Remove all spaces with one line of code. Since spacing should not define whether a number is valid/invalid.
Step 2:
Use this regex
^(?:4[0-9]{12}(?:[0-9]{3})?|(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|3[47][0-9]{13})$
This catches a few more rules on MasterCards.
Source (Shortend expression since fewer types of cards are needed)
Demo

Regex for numerical range, show 1 and 01, but reject 0 and 00

I've puzzled a lot, but can't figure out why my regex doesn't work.
It's for an input which should accept a numerical range between 0 and 40. It should reject 0 and 00, but accept 1 or 01 and further...
Where am I wrong?
\b([1-3]?\d{1}|40)\b
[1-3]?\d{1}
matches 0 because [1-3] is optional and \d of course includes 0. Also, the {1} is a no-op - every token is matched once by definition.
You need something like this:
\b(0?[1-9]|[1-3]\d|40)\b

how to make a regular expressions to accept only digits not started with zero or zero only?

I'm trying to create a regex to accept digits not starting with zero or a single zero digit.
Example matches
0
50
798
Example rejects
01
046
0014
00
0001
My attempt was to use /[0]|[1-9][0-9]*/ to match the values in the following text:
0, 50, 798
01, 046, 0014, 00, 0001
This attempt can be run at http://regexr.com/3bb00
Use following regex :
^(0|[1-9]\d*)$
see Demo https://regex101.com/r/zT8uI2/2
This regex contains 2 part, 0 or [1-9]\d* which is a digit that doesn't starts with zero.
Note that if you want to match your numbers within other texts you need a word boundary instead of start and end anchors :
\b(0|[1-9]\d*)\b
see demo https://regex101.com/r/zT8uI2/3
It seems that you have two cases in your regex:
Match a single zero
Match digits that don't start with zero.
The first case is easy: /0/
The second case is also pretty easy /[1-9]\d*/. The [1-9] matches the digit that is not 0. Then, we can have 0 or more digits.
To get both of these cases, just use a bar to do either or
/0|[1-9]\d*/
Hmm, why not something like..
if(input[0] == '0' && input.size() > 1) // reject
else //accept
Please check this http://regexr.com/3bb09
Took the tip from https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9781449327453/ch06s06.html
and improved it to negate numbers starting with 0.
RE: \b[^0,]*([1-9][0-9]*|0)\b
Text: 0, 50, 798, 01, 046, 0014, 00, 0001
Matched only 0, 50 and 798
Thanks
Venkat

regex to test repeatition of character

I have a specific requirement to check if all the characters in a string of 8 characters contains repeatition of character '0'
I was trying to use regular expression 0{8} to validate for all cases to get result as true -
but above regular expression will validate only *
0
00
000
0000
00000
000000
0000000
00000000 ->*
Can anyone suggest if i need to change something in my regular expression to validate all the above inputs ?
Appreciate your help.
To detect any number of repetitions of 0, simply use:
0+
This matches all your test cases, and of course would match more than 8, but if your input string is max 8 chars, it's OK.
You should use 0{1,8} to catch all cases you've provided.
{8} requires exactly 8 characters. Use a range:
0{1,8}
This should be easy with the following.
a = '000000000'
b = '000001000'
a.strip('0') # returns ''
b.strip('0') # returns '1'
For C++
replace( s.begin(), s.end(), '0', '');