-1

I am trying to replace the substring from a string like:

Sample string value '[1]T.1.v - Marriage Statement'!C10 and this

The text to replace is '[1]T.1.v - Marriage Statement'!C10 with some other string say var1 so that the string becomes Sample string value var1 and this. The problem is there can be strings with values like Sample string value '[1]T.1.v - Marriage Statement'!C101 and this where the replacement should not work. I was exploring the word boundary regex to limit the replacement:

String sample = "Value can be '[1]T.1.v - Marriage Statement'!C10 and this";
String a = sample.replaceAll("\\b'[1]T.1.v - Marriage Statement'!C10\\b", "var1");
System.out.println(a);

But this doesn't seem to replace the word at all. I tried simple string replace but that will replace the substring in Sample string value '[1]T.1.v - Marriage Statement'!C101 and this as well giving output as Sample string value var11 and this which is an error.

Any suggestions how can this be achieved?

Sangram Mohite
  • 735
  • 4
  • 11
  • 23

4 Answers4

4

Remove leading \b then work with \Q...\E escape sequence to treat input string as literals:

String a = sample.replaceAll("\\Q" + param +"\\E\\b", "var1");
revo
  • 47,783
  • 14
  • 74
  • 117
1

Single quote is not considered a word boundary, so the initial \\b anchor prevents the rest of the text from matching.

Moreover, your text itself has a significant number of special characters, so it should be quoted with \\Q and \\E.

Here is a regex that fixes these shortcomings:

"(?<=\\s|^)\\Q'[1]T.1.v - Marriage Statement'!C10\\E(?=\\s|$)"

It replaces the initial \\b with (?<=\\s|^), and the final \\b with (?=\\s|$) to allow for the target string to begin and end in non-word characters, such as quotes. Use of \\Q and \\E allows us to keep special characters in the rest of the target string unescaped.

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thanks for the answer. I just have one small doubt. If the string expression has parentheses around the text to replace or has comma afterwards then it wont work right? Like "Value can be ('[1]T.1.v - Marriage Statement'!C101) and this"; – Sangram Mohite Jun 20 '17 at 17:36
  • 1
    @SangramMohite This would not work for boundaries other than spaces and beginning/ending of the line. If you have a list of characters at which you would like to break the search, simply add them to the list, along with `\\s`. – Sergey Kalinichenko Jun 20 '17 at 17:38
  • FYI `(?<=\s|^)` == `(?<!\S)` – revo Jun 20 '17 at 17:43
  • It lead me to the answer and got the same answer from @revo too! String a = sample.replaceAll("\\Q" + "'[1]T.1.v - Marriage Statement'!C10" + "\\E\\b", "var1"); – Sangram Mohite Jun 20 '17 at 17:43
  • Thanks a lot for the help! It has been helpful! – Sangram Mohite Jun 20 '17 at 17:45
  • 1
    @SangramMohite Removing leading and/or trailing break marks lets you match parts of longer substrings. A better approach is to come up with an explicit list of characters that break a substring, and use them in your regex for lookaheads and lookbehinds. – Sergey Kalinichenko Jun 20 '17 at 17:48
0

Escape brackets and dots

'\[1\]T\.1\.v - Marriage Statement'!C10
Kaushal Niraula
  • 563
  • 3
  • 7
0

Two things:

• change your \\' in your replaceAll to just \'

• change replaceAll(... to replace(... (See bottom of post)

It should look like this:

public static void main(String[]args) {
    String sample = "Value can be '[1]T.1.v - Marriage Statement'!C10 and this";
    String a = sample.replace("\'[1]T.1.v - Marriage Statement\'!C10", "var1");
    System.out.println(a);
}

Yielding the output:

Value can be var1 and this

using replaceAll is better if you are replacing a bunch of occurances of something, such as the letter A. replace is good for replacing something if it is just one instance.

  • I mentioned in the question. I tried this but the problem is that it would replace for substrings '[1]T.1.v - Marriage Statement'!C101 also which is unexpected. – Sangram Mohite Jun 20 '17 at 17:30
  • I tried simple string replace but that will replace the substring in "Sample string value '[1]T.1.v - Marriage Statement'!C101 and this" as well giving output as Sample string value var11 and this which is an error. – Sangram Mohite Jun 20 '17 at 17:30
  • I ran this exactly how it is in the post and it works fine. Your main problem was the `\\'` rather than `\'` in your replacement method(See bullet point 1). –  Jun 20 '17 at 17:34