CLR Regex for fractions - regex

I need a CLR Regex for fractions or whole numbers and fractions where
1/2 is correct
12 2/3 is correct too
and a minus sign can popup just before any number.
I first came up with -?([0-9]* )?-?[0-9]+\/-?[0-9]+ but that seems to allow 2/7 12 too for example.

Well, that regex would be in two parts, the (optional) whole number:
(:?-?\d+ )?
and the fractional part:
-?\d+/-?\d+
And we need to match the complete string; so:
^(:?-?\d+ )?-?\d+/-?\d+$
Testing a bit:
PS> $re=[regex]'^(:?-?\d+ )?-?\d+/-?\d+$'
PS> "1/2","12 1/2","-12 2/3","-5/8","5/-8"|%{$_ -match $re} | gu
True
However, this allows for "-12 -2/-3" as well, or things like "1 -1/2" which don't make much sense.
ETA: Your original regex works, too. It just lacked the anchors for begin and end of the string (^ and $, respectively). Adding those make it work correctly.

I just had a similar requirement, but I wanted to match decimal numbers as well.
I came up with the following regex
^-?(?<WholeNumber>\d+)(?<Partial>(\.(?<Decimal>\d+))|(/(?<Denomiator>\d+))|(\s(?<Fraction>\d+/\d+)))?$
Or just this if you don't want named groups
^-?\d+((\.\d+)|(/\d+)|(\s\d+/\d+))?$
To remove the decimal number from validating, it would be shortened to
^-?\d+((/\d+)|(\s\d+/\d+))?$
Hope this helps someone!

try this , just for single fraction and not the whole number
/^\d{1}\/?\d{1}$/

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?\\",

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)

Regular Expression for a 0.25 interval

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).

Regular expression to match a percentage with decimal?

I'm attempting to build a regular expression (.NET) to match a percentage with 4 decimal places. The 4 decimal places are required. The range looks like:
0.0001 to 100.0000
So far, I've come up with:
(?:[^0]+[1-100]{1,3})\.(?:\d{4})
However, I'm a bit unsure on how to add a few other requirements to this expression. I need:
No leading zeroes before the decimal point. 42.4214 is allowed, 042.4214 is not. 1.0000 is allowed but 001.0000 is not. Etc..
Up to 3 characters allowed before decimal without leading zeroes.
If the number before the decimal is 100, do not allow the number after the decimal to be anything other than 0000, so 100.0000 is allowed, but 100.0135 is not allowed. (Is this even possible with a regex?)
Help is appreciated!
Just treat the 100.0000 possibility as a separate case. It's easy to match that (100\.0000), and it's easy to match the rest ([1-9]?\d\.\d{4}), so with the two as alternates you get:
^(100\.0000|[1-9]?\d\.\d{4})$
(Assuming you want it to be the whole text, otherwise leave out the ^ and the $.
I would do something like this:
^((([0-9]|[1-9]\d)\.\d{4})|100\.0000)$
Proof
See it in action:
^(0|[1-9]\d?)\.\d{4}|100\.0000$
Matches:
0.0001
100.0000
42.4214
1.0000
Doesn't match:
100.0135
042.4214
001.0000
000.0000
1000.0000
2000.0000
300.0000

Regex for decimal with 1 to 9 digits before decima sign and 1 or 2 after decimal sign

Looking for regex which will strictly allow 1 to 9 digits before decimal dot(.) and 1 or 2 digits after dot(.)
It should validate
65564.54
654654654.45
1.55
But not
556
654654654
65455.6544
55.566
I have tried ^[0-9]{1,9}\.[0-9]{1,2}$
^\d{1,9}\.\d{1,2}$
http://rubular.com/r/IdYgiwNmoH
And for numbers not separated by linebreaks:
\b\d{1,9}\.\d{1,2}\b
http://rubular.com/r/yVFJKsqsiZ
The expression you've tried works fine, as long as you intend the number to be the entire line. If you're intending that the value be part of the line amongst other things, your ^ and $ are the problems.
Your regex is fine. I'm not sure what langauge you are working with, but you can verify javascript regex with Regex Pal:
http://regexpal.com/
Regular Expressions.info has a great built-in VB checker as well:
http://www.regular-expressions.info/vbscriptexample.html