Regular Expression for a 0.25 interval - regex

My aim is to write a regular expression for a decimal number where a valid number is one of
xx.0, xx.125, xx.25, xx.375, xx.5, xx.625, xx.75, xx.875 (i.e. measured in 1/8ths) The xx can be 0, 1 or 2 digits.
i have come up with the following regex:
^\d*\.?((25)|(50)|(5)|(75)|(0)|(00))?$
while this works for 0.25,0.5,0.75 it wont work for 0.225, 0.675 etc .
i assumed that the '?' would work in a case where there is preceding number as well.
Can someone point out my mistake
Edit : require the number to be a decimal !
Edit2 : i realized my mistake i was confused about the '?'. Thank you.

I would add another \d* after the literal . check \.
^\d*\.?\d*((25)|(50)|(5)|(75)|(0)|(00))?$

I think it would probably just be easier to multiply the decimal part by 8, but you don't consider digits that lead the last two decimals in the regex.
^\d{0,2}\.(00?|(1|6)?25|(3|8)?75|50?)$

Your mistake is: \.? indicates one optional \., not a digit (or anything else, in this case).
About the ? (question mark) operator: Makes the preceding item optional. Greedy, so the optional item is included in the match if possible. (source)

^\d{0,2}\.(0|(1|2|6)?25|(3|6|8)?75|5)$

Regular expressions are for matching patterns, not checking numeric values. Find a likely string with the regex, then check its numeric value in whatever your host language is (PHP, whatever).

Related

regex for a string ,\"16 questions\",

I am not good in regex and I spent so much time figure out how to search for the below pattern:
,\"16 questions\",
This is what I constructed .\"[0-9,]+ questions\".
I think I am close but not sure how much. Can someone please correct it. The numeric value can have comma in it when the number crosses 1k. e.g 2,500 questions.
,"\d{1,3}(,\d{3,3})*\squestions?",
Explanation:
\d{1,3}= 1~3 decimal digits
(,\d{3,3})* = comma and 3 decimal digits, the whole group repeating 0~N times
\s = whitespace
s? = letter s can be missing
These two parts give you accurate recognition of possible numbers.
▶ Test and visualization.
If the backslashes in your text are true backslashes, then the regex including them would be
,\\"\d{1,3}(,\d{3,3})*\squestions?\\",
This works. You didn't indicate if the numeric value could have more than 1 comma (e.g. 1,000,000)
,\\"((\d{1,3})(,\d{3})*)\squestions?\\",

How can I use REGEX to test for currency formats

How can you create a regular expression that checks if a user input matches characters formally found in a currency syntax? (number, period/decimal place, comma, or dollar sign?).
The following can find all characters listed above except for the dollar sign, any idea how to properly structure this?
/([0-9.,])/g
The regex I use for currency validation is as follows:
^(\$)?([1-9]{1}[0-9]{0,2})(\,\d{3})*(\.\d{2})?$|^(\$)?([1-9]{1}[0-9]{0,2})(\d{3})*(\.\d{2})?$|^(0)?(\.\d{2})?$|^(\$0)?(\.\d{2})?$|^$
RegExr is a great website for testing and reviewing these strings (perhaps you could make a regex string that's less of a beast!)
Are you just trying to test the characters? In that case
[0-9,.$]+
will suffice. Or are you testing for the format $1,123,123.12 with the correct placements of commas and everything?
In that case you would need something more like
(\$?\d{1,3}(?:,\d{3})*(?:.\d{2})?)
should do.
You need to define what you want your regex to match, more formally than "matches characters formally found in a currency syntax". We don't know which currencies you're interested in. We don't know how strict you need it to be.
Maybe you'll come up with something like:
These elements must come in this order:
A currency symbol ('£', '€' or '$') (your requirement might specify more currencies)
1 or more numeric digits
A period or a comma
Exactly two numeric digits
Once you have a specification like that, it's easy to translate into a regular expression:
[£€$] // one of these chars.
\d+ // '+' means 'one or more'
[.,] // '[]' means 'any one of these'.
\d\d // Two digits. Could also be written as '\d{2}'
Or concatenated together:
[£€$]\d+[.,]\d\d
If you've learned about escaping special characters like $ and ., you may be surprised not to see it done here. Within [], they lose their special meaning.
(There are dialects of regex -- check the documentation for whatever implementation you're using)
Your requirements may be different though. The example I've given doesn't match:
$ 12.00
$12
USD12
¥200.00
25¢
$0.00005
20 μBTC
44 dollars
£1/19/11¾d ("one pound, nineteen shillings and elevenpence three farthings")
Work out your requirement, then write your code to meet it.
you should set \ before special chars, also you should set star(0+) or plus(1+) for match full currency chars, for example:
/([0-9\.,]*)/g
or for real price how 200,00 where all time exist 2 symbols after comma:
/(([0-9]+)(\.|,)([0-9]){2})/g

RegEx which accepts only two decimal places

Hi I am working on RegEx. Correct response should NOT allow for number to the tenths only, as in RESPONSE = "925.0", nor should it allow for trailing zeros after the hundredths place as in RESPONSE = "925.000". Only correct responses: 925, 0925, 0925., 925., 925.00, 00925
I worked on it and finally came up with this
"^-?(0)*(\d*(\.(00))?\d+.|(\d){1,3}(,(\d){3})*(\.(00))?)$"
It works for three digit numbers but if i want it for 38400.00 it doesn't allow it
I am not quite certain whether the decimal places can be any digit or if they have to be zero. If the former, then this should do the trick:
^-?\d{1,3}(,?\d{3})*(\.(\d{2})?)?$
If the latter, then this:
^-?\d{1,3}(,?\d{3})*(\.(00)?)?$
The entire match starting with the decimal point is optional, and the two decimal places in that match are optional as well.
UPDATE I just realized that it appears you need to accept commas in the response as well - I assume for thousands, millions, etc.
UPDATE #2 per OP's comment
^-?(\d+|\d{1,3}(,\d{3})*)(\.(00)?)?$
UPDATE #3 Added link to regex101 for explanation of this regular expression.
Have a try with:
^-?\d{1,3}(?:,?\d{3})*(?:\.(?:00)?)?$
I think your problem is that you're trying to match it in chunks of three, with commas separating, but 38400.00 doesn't have commas.
Try this:
^-?\d+(\.?(\d{2})?)$
The - indicates the character, -. With the ? after, it says that it may or may not apply. This allows negative numbers, so if you only want positive numbers matched, delete the first two characters.
\d represents every digit. The + after says that there can be as many as you want, as long as there's at least one.
Then there's a \., which is just a dot in the number. The ? does the same as before.. Since you seem to allow trailing periods, I assumed you wanted it to be considered separately from the following digits.
The () encloses the next group, which is the period (\.) followed by two characters that match \d -- two digits -- and which may be repeated 0 or 1 times, as dictated by the ?. This allows people to either have no digits after the period or two, but nothing else.
The ^ at the beginning specifies it has to be at the beginning of the line, and the $ at the end specifies it has to end at the end of the line. Remember to enable the multiline (m) flag so it works properly.
Disclaimer: I've not done much regex work before, so I could well be totally off. If it doesn't work, let me know.
Couldn't you do this without the ?'s
^[0-9,]+(\.){0,1}(\d{2}){0,1}$
improved: ^\d+[0-9,]*(\.){0,1}(\d{2}){0,1}$
Edit:
Broken down a bit as requested
Old one:
[0-9,]+
1 or more digits/commas (would have accepted ',' as true) so improved version:
\d+
for starts with 1 or more digits
[0-9,]*
0 or more digits/commas
followed by
(\.){0,1}
0 or 1 decimal
Followed by
(\d{2}){0,1}
0 or 1 of (exactly 2 digits)

Regex expression to catch variant of a word

I am looking for simple way to use regex and catch variant of word with simplest format.
For example, the 5 variants of the word below.
hike
hhike
hiike
hikke
hikkee
Using something similar to the format below...
[([a-zA-Z]){4,}]
Thanks
Are you looking for something like /h+i+k+e+/?
Meaning:
The literal h character repeated 1 to infinity times
The literal i character repeated 1 to infinity times
The literal k character repeated 1 to infinity times
The literal e character repeated 1 to infinity times
DEMO
If each character can maximum be there twice, you can use /h{1,2}i{1,2}k{1,2}e{1,2}/ meaning "present 1 or 2 times".
You probably cannot solve this generically (i.e. for any word) under standard regex syntax.
For a given word, as others have pointed out, it is trivial.
This is more of a soundex kind of problem I think:
https://stackoverflow.com/a/392236/514463

regular expression allows only decimal to enter

^(0)?[0-9]{0,}((\.){1}[0-9]{0,2}){0,1}$
The above regular expression allows me to enter numbers upto 4 decimal places with an optional 0 before the decimal(.) point. But it is not throwing error when I'm not entering numbers after decimal like 0., 12.,etc. !!!
Any guidance on this.
This is because you allow between 0 and 2 digits after the ..
Since you said you wanted between 1 and 4 digits, you probably want this:
^[0-9]*(\.[0-9]{1,4})?$
Note, I have replaced {0,} with * and {0,1} with ? as they are equivalent.
In addition, I've removed the {1}, as it matches one time per default.
The initial (0)? is also a bit redundant.
Beware, this regular expression matches .50. If you want to ensure that a number exists before the decimal point, change the first * to a + like so:
^[0-9]+(\.[0-9]{1,4})?$
To further simplify the regular expression, you can also replace [0-9] with \d in most regular expression engines, giving you:
^\d+(\.\d{1,4})?$
This regex could be simplified to the following:
^\d*(\.\d{1,4})?$
You can see it in action here: http://regexr.com?2vamh