472,958 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Regular explression help - fractions

C#

I am writing a function that allows the user to enter fractions as answers
(i.e. how many asprin tablets for a dosage?).

I have the first half of the Regex figured out but don't know how to do the
second half. In the snippet "1-2 1/2". I can figure out the part before the
dash but not after. The second part is optional for the user. If it is there
it must be complete
Any hints?

"^([0-9]+|[0-9]+\/[1-9]+|[0-9]+ [0-9]+\/[1-9]+)$"

t0 = "1" - pass
t1 = "1-1" - pass
t2 = "1/2" - pass
t3 = "1 1/2" - pass
t4 = "1 1/2-2" - pass
t5 = "1 1/2-2 1/2" -pass

f1 = "1-" - fail
f2 = "1 1/2-" - fail
f3 = "1/2 1/2" - fail
f4 = "1/2-1/2-" - fail
f5 = "2-1" - fail ??
Jul 21 '05 #1
2 1327
Stedak wrote:
t0 = "1" - pass
t1 = "1-1" - pass
t2 = "1/2" - pass
t3 = "1 1/2" - pass
t4 = "1 1/2-2" - pass
t5 = "1 1/2-2 1/2" -pass

f1 = "1-" - fail
f2 = "1 1/2-" - fail
f3 = "1/2 1/2" - fail
f4 = "1/2-1/2-" - fail

f5 = "2-1" - fail ??

// IgnorePatternWhitespace|Explicitcapture
^
( (\d+ \s* (\d+ \s* \/ \s* \d+ )? ) | # ln 1
(\d* \s* (\d+ \s* \/ \s* \d+)) ) # ln 2
( # ln 3
\s* - \s* # ln 4
( (\d+ \s* ( \d+ \s* \/ \s* \d+ )?) | # ln 5
(\d* \s* ( \d+ \s* \/ \s* \d+ ) )) # ln 6
)?
$

// 1: a whole number followed by optional fraction OR
// 2: optional whole number followed by mandatory fraction
// 3: begin optional group
// 4: a dash, with optional white space on either side
// 5: a whole number followed by optional fraction OR
// 6: optional whole number followed by mandatory fraction
// 7: end optional group

--

www.midnightbeach.com
Jul 21 '05 #2
> > f5 = "2-1" - fail ??

Oops, I meant to add that I don't think that there's any way for a
regex to act on the 'meaning' of a match - just match / no-match.

--

www.midnightbeach.com
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: selowitch | last post by:
I've been searching in vain for a way to present typographically correct fractions (not resorting to <sup> and <sub> tags) but have been frustrated by the fact that the glyphs for one-half,...
5
by: Steffen | last post by:
Hi, is it possible to have two fractions, which (mathematically) have the order a/b < c/d (a,b,c,d integers), but when (correctly) converted into floating point representation just have the...
2
by: Mori | last post by:
Hi, Can someone supply a code example of displaying a string with a fractional part, say 5 and 7 16ths. I cannot find an example of how to use the Encoding object (if that is what you use). ...
3
by: Ori | last post by:
Hi , I'm working with C#.NET and I'm looking for the following. I have a web page content and I want to pull all the text which appear in the page without all the HTML tags. I know that there...
2
by: Stedak | last post by:
C# I am writing a function that allows the user to enter fractions as answers (i.e. how many asprin tablets for a dosage?). I have the first half of the Regex figured out but don't know how to...
4
by: Bob | last post by:
Hi All, Was wondering if in C# there is an elegant way of displaying and or calculating fractions. The case: we have an app that measures/slices dices etc and all our internal measures and...
1
by: Semajthewise | last post by:
Here it is cleaned up a little more. Here's what this code does. It will take 2 fractions and add, subtract, multiply, or divide them. The user enters the fractions to be calculated into two...
5
by: gubbachchi | last post by:
Hi all, How to convert the fractions into decimals and vice versa in php. I have a form, where the user will enter fractions in the text boxes such as "1 1/2", "1 1/4" and so on. I need to store...
0
by: Paddy | last post by:
(From: http://paddy3118.blogspot.com/2008/09/python-fractions-issue.html) There seems to be a problem/difference in calculating with the new fractions module when comparing Python 26rc2 and 30rc1...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.