0

Hi I am new to regular expression. I have string like this

"( NATIVE_WHERE_CLAUSE = 'UnitOfMeasure.MeasurementType=[Weight]' ) AND ( NATIVE_RELATION_WHERE_CLAUSE = 'Reference_Name=[Nut to coolent oil]' )"

I tried replace the square brackets [] with single quote ' with replaceAll() method. But it did not work.

Can any one help me what will be regular expression for replacing the square brackets [] with single quote in my above string.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Kuls
  • 61
  • 6
  • That expression will create a double ' in the string: `...Type='Weight'' ) AND ...`. Are you sure you want to do that? You probably want something like `#[\[\]]/\\'#` (`:%s/[\[\]]/\\'/g` if you do that with `sed` or inside `vim`). – LSerni Jun 12 '17 at 05:40
  • Possible duplicate of [Java Regular expression matching square brackets](https://stackoverflow.com/questions/43246115/java-regular-expression-matching-square-brackets) – Wiktor Stribiżew Aug 02 '17 at 17:33

1 Answers1

0

\\[\\] replace [] in a string. And use below to replace [] with '

"( NATIVE_WHERE_CLAUSE =  'UnitOfMeasure.MeasurementType=[Weight]'  ) AND ( NATIVE_RELATION_WHERE_CLAUSE =  'Reference_Name=[Nut to coolent oil]'  )".replace(new RegExp('\\[|\\]', 'g'), "'");

but that creates two single quotes, one as a child of other. That will not work. So, you must replace with \' to avoid the error as shown below.

"( NATIVE_WHERE_CLAUSE =  'UnitOfMeasure.MeasurementType=[Weight]'  ) AND ( NATIVE_RELATION_WHERE_CLAUSE =  'Reference_Name=[Nut to coolent oil]'  )".replace(new RegExp('\\[|\\]', 'g'), "\\'");
Mr_Perfect
  • 8,254
  • 11
  • 35
  • 62