-1

I am expecting the following regex to allow 0-n spaces between operators, but it is forcing at least one. Can someone please correct the error of my ways?

((\d+\.?\d*|\d*\.?\d+\s+?[\+\-\/\*]\s+?)+)(\d+\.?\d*|\d*\.?\d+)

Examples

24*3.2
24 * 3.2

Only the 2nd example is allowed through.

I understood \s+? should be an optional number of spaces?

Playpen

jenson-button-event
  • 18,101
  • 11
  • 89
  • 155

2 Answers2

1

+? (or more generally ? following any other quantifier) is a non-greedy quantifier. It does not mean “make the preceding match optional”.

Use * instead of +?.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
0

What you need is \s*, i.e., 0 or more white spaces. Your \s+? says 1 or more white spaces and non greedy match for spaces. The ? modifier after * or + means non-greedy match.

Prashanth
  • 26
  • 5