^[ -]?\d*\.?\d (?:[ x\-÷]?\d*\.?\d )*[ x\-÷]?$
CodePudding user response:
The regex you posted above is getting quite long and it's not clear what your intended logic is for all the parts so I've simplified for now.
This will match the types of numbers you describe:
^(\d\.?(\d )?)$
https://regex101.com/r/nFxfs5/1
And here it is with some basic addition to demonstrate:
(\d\.?(\d )?)\ (\d\.?(\d )?)
https://regex101.com/r/hpNp9p/1
It looks like you may need to review what you've written so far, at a glance there may be too many ? in there: for example isn't the [ x\-÷] part necessary to move to the next state? If you haven't been doing so, I recommend making a longer list of test cases in the regex tester in advance as I have in the links above so it's easier to see the effect of each change you make as you go.
CodePudding user response:
Try this regex:
^\d (?:\.\d )?(?:\s*[ x\/÷-]\s*\d (?:\.\d )?)*$
Explantion:
^- matches the start of the line\d (?:\.\d )?- matches 1 or more digits followed by optional decimal and fractional part. So, this subpattern will match numbers line9,3.456etc(?:\s*[ x\/÷-]\s*\d (?:\.\d )?)*\s*- matches 0 or more whitespaces[ x\/÷-]- matches the operators like,x,/,-,÷\s*- matches 0 or more whitespaces\d (?:\.\d )?- matches 1 or more digits followed by optional decimal and fractional part
$- matches the end of the line

