User5590 User5590 - 6 months ago 36
C# Question

Split the string using Regex with the special characters

I have a string as [@variable] -eq 'some value' or 'some value' -eq [@variable] and i want to split it using Regex.

The expected output is as below

  1. [@variable]

  2. -eq

  3. 'some value'


  1. 'some value'

  2. -eq

  3. [@variable]

I have the used regular expression \w+|'[\w\s]*' to match and split the string but it gives me below output

  1. variable

  2. eq

  3. some value

which ignores the special characters with the string.

Can anyone please help me on this?


The answer by lbrahim is working for most of the scenario but failing when the value is date time.

[@variable] -eq 'MM/dd/yyyy'

The above expression should also work for the same


Try this:



Or this: