3

I'm trying to write a program that will take in a string and use RegEx to search for certain mathematical expressions, such as 1 * 3 + 4 / 2. Only operators to look for are [- * + /].

so far:

string = "something something nothing 1/ 2 * 3 nothing hello world"

a = /\d+\s*[\+ \* \/ -]\s*\d+/

puts a.match(string)

produces:

1/ 2

I want to grab the whole equation 1/ 2 * 3. I'm essentially brand new to the world of regex, so any help will be appreciated!

New Information:

a = /\s*-?\d+(?:\s*[-\+\*\/]\s*\d+)+/

Thank you to zx81 for his answer. I had to modify it in order to work. For some reason ^ and $ do not produce any output, or perhaps a nil output, for a.match(string). Also, certain operators need a \ before them.

Version to work with parenthesis:

a = /\(* \s* \d+ \s* (( [-\+\*\/] \s* \d+ \)* \s* ) | ( [-\+\*\/] \s* \(* \s* \d+ \s* ))+/
Delliardo
  • 175
  • 2
  • 3
  • 15
  • 1
    Please clarify what you mean by a mathematical expression. Can it contain, for example, symbols like `^`, Greek letters, words like `log`, etc. Frankly, I'm doubtful that you're going to be able to turn this into an unambiguous question. – Cary Swoveland Jun 28 '14 at 02:44
  • 1
    @Fabric, I think that's a different question. It would address a string like `"/**123**/"`, but that's not a mathematical expression. – Cary Swoveland Jun 28 '14 at 02:50
  • if you don't care about parenthesis: `-?\d+(\s?[\+\-\*\/]\s?-?\d)+` [demo](http://regex101.com/r/qT5rN8) – Fabricator Jun 28 '14 at 02:52
  • Terribly sorry for not saying what kind of mathematical equations. I was worried more about my grammar! The only operators I'm worried about are '+', '/', '*', and '-', along with numbers and parenthesis. Major oversight on my part! – Delliardo Jun 28 '14 at 04:46
  • Delliardo, you are forgiven, but just this once :-) Your mind-meld with @zx81 seems to have worked, however. I'm glad you got the answer you wanted. Not too late to do an edit to clarify your question. Many others may read this in future, and they would be appreciative. – Cary Swoveland Jun 28 '14 at 07:21

1 Answers1

3

Regex Calculators

First off, you might want to have a look at this question about Regex Calculators (both RPN and non-RPN version).

But we're not dealing with parentheses, so we can go with something like:

^\s*-?\d+(?:\s*[-+*/]\s*\d+)+$

See demo.

Explanation

  • The ^ anchor asserts that we are at the beginning of the string
  • \s* allows optional spaces
  • -? allows an optional minus before the first digit
  • \d+ matches the first digits
  • The non-capturing group (?:\s*[-+*/]\s*\d+) matches optional spaces, an operator, optional spaces and digits
  • the + quantifier matches that one or more times
  • The $ anchor asserts that we are at the end of the string
Community
  • 1
  • 1
zx81
  • 41,100
  • 9
  • 89
  • 105
  • FYI added demo and explanation – zx81 Jun 28 '14 at 02:59
  • @CarySwoveland Maybe this one passes your keen scrutiny ;) – zx81 Jun 28 '14 at 03:00
  • Your regex failed when I applied it to the mathematical expression that is central to the [Riemann–Lebesgue lemma](http://en.wikipedia.org/wiki/Riemann%E2%80%93Lebesgue_lemma). Sorry. – Cary Swoveland Jun 28 '14 at 03:26
  • @CarySwoveland Sorry not sure if I should detect irony. :) Are you saying it's failing the OP's requirements, or the monster on that Wikipedia page? – zx81 Jun 28 '14 at 03:34
  • 1
    Just making the point that "any mathematical expression" is a rather broad term. Presumably, the OP has in mind some very limited forms of mathematical expressions, but without clarification, we can only guess what they may be. Note also that the OP said nothing about evaluating the expression, just finding it. – Cary Swoveland Jun 28 '14 at 03:46
  • @CarySwoveland Yes... What you said. :) – zx81 Jun 28 '14 at 03:54
  • I was overlooking the optional operator this whole time! I thank you for this answer! I had to modify your regex, however. Look in the original post as I don't want to clutter this down here. – Delliardo Jun 28 '14 at 04:48