1

I would like to validate my infix notation through regex. Right now I have something like this:

^[a-zA-Z0-9_]+(( [a-zA-Z0-9_]+)|( [\+\-\/\*]))*( [a-zA-Z0-9_]+)$

I check if the expresion starts and ends with operands. In the middle i can have operands or operators always devided by single space. There could be only one operator and operands can have multiple characters. Everything seems to be ok for now. But I would also like to check if operands are always separated by operator and I have no idea how to achieve that. Could someone give me some advice?

androlama
  • 63
  • 9

1 Answers1

0

You may use the following fix:

^[a-zA-Z0-9_]+(?: *[+\/*-] *[a-zA-Z0-9_]+)*$

See the regex demo.

Note that depending on the regex library you may or may not use \w instead of [a-zA-Z0-9_], as in .NET or Python 3.x \w is Unicode aware by default.

Pattern details

  • ^ - start of string
  • [a-zA-Z0-9_]+ - 1 or more ASCII letters, digits or _
  • (?: *[+\/*-] *[a-zA-Z0-9_]+)* - zero or more sequences of:
    • *[+/*-] * - zero or more space, an operator, 0+ spaces,
    • [a-zA-Z0-9_]+ - 1 or more ASCII letters, digits or _
  • $ - end of string.

Note that in case your regex flavor is not using regex delimiters, there is no point escaping /. To match any whitespace, replace the regular space in the pattern with \s.

The (?:...) construct is a non-capturing group. It is meant to group patterns, either alternations (like (?:abc|xyz)) or quantified (like here), or both. The value it matches is not stored anywhere, you cannot access it after a match is found (unlike the capturing group).

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563