Regex: How to replace a ColumnName with tablename.ColumnName using BRE regex in java

I have a condition string:

condition = "PlanSliceGUID='[PlanSliceGUIDMV]' and PlanSliceGUID > 'uhj'"

Where PlanSliceGUID is columnName and '[PlanSliceGUIDMV]' is a variable. I just need to replace ColumnName i.e. PlanSliceGUID with tableName.PlanSliceGUID excluding the '[PlanSliceGUIDMV]'.

String should look like this after replacement:

tableName.PlanSliceGUID='[PlanSliceGUIDMV]' and tableName.PlanSliceGUID > 'uhj'

Any suggestion is appreciated.


Use this regex:


and replace it with:


Full example:

String condition = "PlanSliceGUID='[PlanSliceGUIDMV]' and PlanSliceGUID > 'uhj'";
        System.out.println("Result: "+ condition.replaceAll("\\bPlanSliceGUID\\b","tableName.$0"));

Live demo:

You can use this regexp:

condition = condition.replaceAll("([^\\[]|^)(PlanSliceGUID)", "$1tableName.$2");

It allows you to replace PlanSliceGUID by tableName.PlanSliceGUID when PlanSliceGUID is not preceded by [. So, it also works if the variable is named [PlanSliceGUID].

If you want it to be case-insensitive, and replace with the same case, you have to use the i flag:

condition = condition.replaceAll("([^\\[]|^)(?i:(PlanSliceGUID))", "$1tableName.$2");

If you want it to be case-insensitive, but to replace with a fixed case, you have to specify what you want to replace with instead of $2:

condition = condition.replaceAll("([^\\[]|^)(?i:(PlanSliceGUID))", "$1tableName.PlanSliceGUID");

Need Your Help

Read data from a SQL server sql-server

I have a Table with the name "MyTable". I want to read the "Active3" column When the "ID3" column is an specified value. But I get this error: Invalid attempt to read when no data is present.

How to call a node in a hook menu?

drupal menu drupal-7

I hope you can help me because I don't know how to call a node in a hook_menu in Drupal 7.