-7

I'm looking for a regular expression that can match integers, decimals and fractions from a string:

Here are some examples

11.325 x 55.65
11x13
11”x13”
11” x 13”
11 3/8 x 15 7/8 
11 3/8” x 15 7/8“

Explanation of above examples:

  1. Two decimals
  2. Two integers
  3. Two integers with quote to represent inches
  4. Same as above but with spaces
  5. Two integers each followed by a fraction
  6. Same as above with a quote after fraction for inches

So far I have come up with code that will match any numeric values, i.e. integers and decimals, but I'm stuck on the fractions.

Dim matches = Regex.Matches(curCellVal + "", "[\d.]+")

The above code gives me 6 matches for 11 3/8” x 15 7/8“, I need it to give me 2.

I'm not competent with regex so any help would be much appreciated.

My language of preference is VB.net.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
boruchsiper
  • 2,016
  • 9
  • 29
  • 53
  • Are you looking to match each number individually, or the entire string as a single match? – Steven Doggart Jan 29 '14 at 21:48
  • each number individually. – boruchsiper Jan 29 '14 at 21:49
  • 3
    I have a vague impression that people won't appreciate your question. There's no effort shown, there are many formats and it's not clear what you want to obtain. – Szymon Jan 29 '14 at 21:50
  • You might start [here](http://stackoverflow.com/a/4247184) – HamZa Jan 29 '14 at 21:58
  • 4
    My advice: become competent. The way to solve regexp problems is to very carefully describe the "language" -- the set of strings -- you intend to accept. Instead of writing examples, write English descriptions that use OR a lot. You want to accept the digit zero, OR any non-zero digit followed by any number of digits, OR any digit followed by a point followed by one or more digits, OR... list *all* the cases. Once you have all the cases it is much easier to write a regexp. – Eric Lippert Jan 29 '14 at 22:01
  • Positive examples + textual description thereof != specification, not even a vague one. – John Dvorak Jan 29 '14 at 22:19
  • `[\/\d.]+` would include the front slash in your matches. Is that what you're looking for? – brandonscript Jan 29 '14 at 22:20
  • 1
    [Here you go](http://regex101.com/r/uZ9pP4) for all the pain you've gone through :) – HamZa Jan 29 '14 at 22:22
  • @remus Almost, `10 3/8` should be one match, since it's a number and a fraction. only including the slash would make it into 2 matches. – boruchsiper Jan 29 '14 at 22:24
  • Do you need to capture the quote characters if they appear or just the numbers? – p.s.w.g Jan 29 '14 at 22:27
  • @HamZa Thanks so much, but this regex is for php. I'm looking for vb.net – boruchsiper Jan 29 '14 at 22:28
  • @p.s.w.g Just the numbers. – boruchsiper Jan 29 '14 at 22:30
  • 1
    @boruchsiper How about trying to adapt the information you've been given so far to your particular situation. We're not here to do your work for you. – Bart Jan 29 '14 at 22:30
  • 1
    @boruchsiper Why don't you test it out ? It should work. I also tested it on [regexhero](http://regexhero.net/tester/) but since I need an account there to share a link I've just provided another online tester. – HamZa Jan 29 '14 at 22:31

2 Answers2

5

Give this one a shot:

(\d+[\/\d. ]*|\d)

http://regex101.com/r/oO9yI9

In the future, I'd suggest making your question more clear so we can actually understand what you're trying to do -- provide inputs, expected outputs, and include the programming language you're using.

vb.net is PCRE compliant, so you should be able to use this:

Dim regex As Regex = New Regex("(\d+[\/\d. ]*|\d)")
Dim match As Match = regex.Match("11.325 x 55.65")
If match.Success Then
    Console.WriteLine(match.Value)
    # this matches, so you'll get a value
End If
brandonscript
  • 68,675
  • 32
  • 163
  • 220
3

This regular expression should work:

@"\d+(\.\d+|\s+\d+/\d+)?"

This matches any sequence of one or more digits, optionally followed by an either a . followed by one or more digits or a sequence of one or more whitespace characters, followed by one or more digits, followed by a /, followed by one or more digits.

For example:

Dim inputs = New String() { _
    "11.325 x 55.65", _
    "11x13", _
    "11””x13””", _
    "11”” x 13””", _
    "11 3/8 x 15 7/8 ", _
    "11 3/8”” x 15 7/8““" }
For Each input in inputs
    Console.Write(input + ": ")
    For Each match as Match in Regex.Matches(input, "\d+(\.\d+|\s+\d+/\d+)?")
        Console.Write(" (" + match.Value + ") ")
    Next
    Console.WriteLine()
Next

Produces the output:

11.325 x 55.65:  (11.325)  (55.65) 
11x13:  (11)  (13) 
11"x13":  (11)  (13) 
11" x 13":  (11)  (13) 
11 3/8 x 15 7/8 :  (11 3/8)  (15 7/8) 
11 3/8" x 15 7/8":  (11 3/8)  (15 7/8) 
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • The previous version of your answer worked but now it doesn't. It was similar to the expression in @remus answer. Thanks very much anyway! – boruchsiper Jan 29 '14 at 23:08
  • 1
    @boruchsiper I don't understand what you mean. The only thing I changed in this answer was how I printed the results. – p.s.w.g Jan 29 '14 at 23:10
  • I believe this is what you wrote in the previous answer `\d+[\/\d. ]*\d`. Maybe it was someone else? – boruchsiper Jan 29 '14 at 23:15
  • 1
    @boruchsiper Answers with the same score may be randomly order, so you were probably looking at the other answer. In any case, I'd be happy to correct this answer if you can tell me why it doesn't work. – p.s.w.g Jan 29 '14 at 23:17
  • Oops my bad, just checked again and it does work. I already awarded the answer to @remus, but I voted you up. – boruchsiper Jan 29 '14 at 23:25
  • @boruchsiper No problem, I just wanted to make sure I didn't make a mistake somewhere. Glad I could help. – p.s.w.g Jan 29 '14 at 23:26