Click here to Skip to main content
15,886,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
in fact its matching even blank i m stuck why its not working please help.
it should match a numeric or fraction and only one space charcter and also minus - character.

[EDIT] Sample strings from comment
It should match:
2
2/3
2 2/3 (with one space only)


This is the regex:
^[0-9-]*|[0-9-]* {1}[0-9\/]*$
Posted
Updated 21-Dec-14 22:17pm
v4
Comments
George Jonsson 22-Dec-14 3:41am    
Can you present some example strings you try to match?
varun150 22-Dec-14 3:48am    
it should match 2 or 2/3 or 2 2/3 with one space only.
George Jonsson 22-Dec-14 4:19am    
That was not easy to figure out from your description.
I added that info to your question.
Kornfeld Eliyahu Peter 22-Dec-14 3:42am    
This may help you to understand what you have...
http://www.regexper.com/#%5E%5B0-9-%5D*%7C%5B0-9-%5D*%20%7B1%7D%5B0-9%5C%2F%5D*%24
varun150 22-Dec-14 3:49am    
im new to regex i still do not understand diagram at regexper.com

Even (almost) all is said about the topic, I still have a comment and a possible solution:

  1. your expression fails since you use * instead of + as multiplier
  2. your expression also fails since you use anchors (^...$) but between the anchors, you have an alternative (a|b) - so, the first anchor belongs to the first alternative, the second to the second alternative. I.e. you say: the text may start with zero or more digits or minus signs (i.e. since using the * multiplier, allow to leave that part away, so allowing for any string after that), or it must end on some mixture of digits and spaces (minimal: it ends on one space)
  3. a possible solution for "optional sign followed by integral number with optional quotient, or quotient only": ^[-+]?\d+(?:(?:\s\d+)?\/\d+)?$ (see www.regexper.com[^])

Cheers
Andi
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 22-Dec-14 5:18am    
The only problems is the use of \d it will allow 0 and 0/0 too...
Andreas Gieriet 22-Dec-14 8:00am    
That's where you reach the limit of regex, I'd say.
Regex is useful for checking the form, not the content (i.e. the correct value range, etc.).

The OP did not ask for any numeric constraints - he was confused that the regex did not match - reason: missing parenthesis since anchors bind more than the alternative operator, and the * multiplicator operand also matches zero items.

E.g. the following are formally correct but probably not desired values that must be checked with other means:

-0
0/0
1/0
0/1
0 1/2
1/123456789012345678901234567890
123/1
2/2
etc.

Cheers
Andi

PS: In your regex solution with the attempt to also check for proper value ranges, you fail to cover e.g. 0. Trying to check for proper value ranges renders regex patterns to become extremely complex.
According to your comment the regex you are looking for is this:
http://www.regexper.com/#%5E(%5B1-9%5D%5B0-9%5D*)((%5Cs((%5B1-9%5D%5B0-9%5D*)%5C%2F(%5B1-9%5D%5B0-9%5D*)))*%7C(%5C%2F(%5B1-9%5D%5B0-9%5D*))*)%24[^]

^([1-9][0-9]*)((\s(([1-9][0-9]*)\/([1-9][0-9]*)))*|(\/([1-9][0-9]*))*)$

Now let break it apart...
^([1-9][0-9]*)$

This declare a group of numbers where the first digit must be between 1 and 9 and the second can be from 0 to 9 - optionally...
^(\s(([1-9][0-9]*)\/([1-9][0-9]*)))*

This part allows (means this is optional) a single whitespace (\s) and two numbers (like the first one) separated by /...
^(\/([1-9][0-9]*))*$

The last part allows (again it is optional) a / and a number, same like at the beginning...
This will allow 2 and 2/3 and 2 2/3 also
 
Share this answer
 
Comments
Thomas Daniels 22-Dec-14 4:14am    
+5!
Kornfeld Eliyahu Peter 22-Dec-14 4:15am    
Thank you...
varun150 22-Dec-14 4:18am    
thank you so much for help.
i think [0-9] was culprit that matched the null value.
varun150 22-Dec-14 4:20am    
what if this fraction or non fraction have - or + sign like +3 or -3/5 or -1 3/5
Kornfeld Eliyahu Peter 22-Dec-14 4:23am    
I think that at this point it's your turn :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900