Regular expression to validate time but exclude 0:00 - regex

I'm not an expert to regular expressions, but I tried to validate the user input for a time field with it.
My current regex: /^([0-9]|0[0-9]):[0-5][0-9]$/
It should validate anything positive between 0:01 and 9:59, but it also give positive return to 0:00.
Can you please help me to modify my regular expression to exclude 0:00?
Thanks a lot!

Here is the regex you are looking for.
^((?!(0:00)|(00:00)$)0?[0-9]:[0-5][0-9])$
Uses the concept of negative look ahead. Using negative lookahead, you can say match everything except 0:00 and 00:00.
Hope this helps!

You could use a negative lookahead:
^(?!0?0:00$)0?[0-9]:[0-5][0-9]$

Related

Regular expression - How to exclude certain string from the results set?

I am pretty new in Regular Expression.
Input
/shop/earrings
/shop/yellow-gold-earrings
/shop/white-gold-earrings
/shop/rose-gold-earrings
/shop/necklaces
/shop/yellow-gold-necklaces
/shop/white-gold-necklaces
/shop/rose-gold-necklaces
/shop/best-buy-earrings
Regular Expression I used
\/shop\/[a-z-]*-?earrings
Desired Result
/shop/earrings
/shop/yellow-gold-earrings
/shop/white-gold-earrings
/shop/rose-gold-earrings
Actual Result
/shop/earrings
/shop/yellow-gold-earrings
/shop/white-gold-earrings
/shop/rose-gold-earrings
/shop/best-buy-earrings
I do not want /shop/best-buy-earrings to be in the result. Please help me to fix the Regular Expression. Thank you.
Simply add gold to the regex before the - and surround with parenthesis:
\/shop\/([a-z-]*gold-)?earrings
Assuming PCRE flavour, you can use:
\/shop\/(?!best-buy)(?:\w+-)*earrings
Or, if you can use other delimiter than slash:
/shop/(?!best-buy)(?:\w+-)*earrings
Demo

regex expression to find words with 0

Please provide a regex expression to find words with 0 like 'A0lytics', 'Alter0tive Medicine'.
But, it should not match words like 'Enterprise 2.0'
If you have this list of words: ["A0lytics", "0tics", "Alter0tive Medicine", "Enterprise 2.0"]
\w+0\w+ will capture "A0lytics" and "Alter0tive" (not "Alter0tive Medicine")
^[A-Za-z][A-Za-z0]+(?: +[A-Za-z][A-Za-z0]+)*$ will match "A0lytics" and "Alter0tive Medicine"
(?<!\.)([a-zA-Z]*)0([a-zA-Z]*) will match "A0lytics", "0tics" and "Alter0tive Medicine" (if you can use the negative lookbehind).
Good luck.
You can use:
^[A-Za-z][A-Za-z0]+(?: +[A-Za-z][A-Za-z0]+)*$
Demo: https://regex101.com/r/gJ5ugU/2
\w+0\w+
is it useful?
You can test online regular expressions with this.

Regular Expression for any decimal

I am having a hard time figuring out a regular expression that would evalute as follows:
(valid)
.123456
1.
1.123456
123456.1
(not valid)
123
0...
I tried the following tool but I am not having much luck. (https://www.regex101.com/)
Thank you!
You could use positive lookahead assertion based regex like below.
^(?=.*\d)\d*\.\d*$
OR
^(?:\d*\.\d+|\d+\.)$
DEMO
^(?!\.$)\d*\.\d*$
Try this.See demo.
https://regex101.com/r/vN3sH3/7

Regex values 0,01 and 9.999.999.999.999,99

I need a regex that allows values between 0,01 and 9.999.999.999.999,99. Any suggestions?
I am using the following regex:
"(?:(?:[0-9]\\d{0,2})(?:\\.\\d{3})*(?:,\\d{2})|0,(?:[0-9]\\d|0[0-9]))"
But it allows 0,00
Thank you for your help
If all you're wanting to do is validate then this regex should work for you.
(?:(?!(?:0{1,3}\.){0,}0{1,3},00)(?:\d{1,3}\.){0,}\d?\d?(?:(0)|\d),\d(?(1)[1-9]|\d))
Regex Demo
I've updated it so that if the value is 0,00 it will not match, but it'll match 10.000,00 or 1,00
The regex will match:
10.000,00
1,00
0,01
10,00
9.999.999.999.999,99
The regex will not match:
0,00
0.000,00
0,01 - 0,99 is a particular case. So is 9.999.999.999.999,99. I would do:
(0,((\d[1-9])|([1-9]\d)))|((([1-9]\d{0,2}(\.\d{3}){0,3})|([1-9](\.\d{3}){4})),\d{2})
Edit
As pointed out by Biffen, 0,x0 is also a special case
I'm not sure regex would be the most appropriate tool for this job here, however I think this is what you are looking for.
(?=[\d\.]*[1-9][\d\.]*,\d{2}|[\d\.]*,[\d\.]*[1-9][\d\.]*)(\d{1,3}\.)*\d{0,3},\d{2}
the positive lookahead enforces the non-zero value
(?=[\d\.]*[1-9][\d\.]*,\d{2}|[\d\.]*,[\d\.]*[1-9][\d\.]*)
and the rest just matches the pattern
(\d{1,3}\.)*\d{0,3},\d{2}
http://regexr.com/39ujm

regex 1-9999 for form validation

I am trying to write some form validation, I need one of the inputs to be 1-9999. I know nothing about regular expressions ( never used them before) and here is my first attempt
/^([1-9][1-9]|[1-9]|[1-9]\d|9999)$/
Does not seem to want to work, can anyone help me? Thanks!
Try the below regex,
^(?:[1-9][0-9]{3}|[1-9][0-9]{2}|[1-9][0-9]|[1-9])$
DEMO
This doesn't exclude zero, but /^\d{1,4}$/ should do the trick.
Try using this
^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])$
To exclude zero values but include non-zero ones with leading zeros:
([1-9]\d{0,3})|(\d[1-9])\d{0,2}|(\d{2}[1-9])\d?|(\d{1,3}[1-9])
This Regex should not match numbers that start with 0 a part from 0
Regex: /^(?!(0\d))\d{1,4}$/
Regex (Exclude Zero): /^(?!(0))\d{1,4}$/
Test: https://regex101.com/r/zfCKel/2