Regular match Just Once (no repetition)

I faced this little regex problem (flags: Global)

[-+*&|]

      

It must match, for example, + in this expression, but not ++ or -:

12 + 47 - i++ / --foo

      

However, it fits all of these.

I am aware of this solution, but it is ugly (needs to be repeated three times):

[^-+*&|][-+*&|][^-+*&|]

      

Or perhaps

(?<![-+*&|])([-+*&|])(?![-+*&|])

      

Any better (shorter and more readable) solution?

+3


source to share


2 answers


this seems a little more readable to me, but not much.

(?<![\+\-\*/\&\|])(?P<operator>[\+\-\*/\&\|])(?!(?P=operator))

      



matches:

+ ++ - -- * ** / // & && | ||
^    ^    ^    ^    ^    ^

      

0


source


[-+*&|]{1,3}

      



you can use {min,max}

to determine the minimum and maximum number of occurrences.

0


source







All Articles