tags:

views:

61

answers:

2

I have the followings string:

String l = "1. [](a+b)\n2. (-(q)*<>(r))\n3. 00(d)\n4. (a+-b)";
String s = "1. [](a+b)\n2. 00(d)"

First string is a expresions list. Sencond string is a expresions subset of first string, but theirs number-ids aren't equals.

Then, I want to do this:

String l2 = "1. <b>[](a+b)</b>\n2. (-(q)*<>(r))\n3. <b>00(d)</b>\n4. (a+-b)";

l2 is a transformation of l but with expressions marked. This marked expresions are contains in s. Note that originals strings have symbols as (, [, and so on What is better way to do it?

+2  A: 

If I understand you correctly, your problem ist that

String.replaceAll(String, String)

doesn't work here, because the Characters (, [, \, have special meanings in regular expressions.

Maybe you could just use

String.replace(CharSequence target, CharSequence replacement)

Then you don't need to deal with regular expressions at all.

fabstab
I know it. The regular expresion is the problem.
isola009
@isola009: Just wanted to acknowledge that using `replace()` should work fine.
Jeff M
@isola009: You do not need any regular expression to implement this. Just use replace().
fabstab
A: 
    String l = "1. [](a+b)\n2. (-(q)*<>(r))\n3. 00(d)\n4. (a+-b)";
    String s = "1. [](a+b)\n2. 00(d)";

    String[] a=l.split("\\n");
    for(int i=0;i<a.length;i++)
        a[i]=a[i].split("\\d. ")[1];
    String[] b=s.split("\\n");
    for(int i=0;i<b.length;i++)
        b[i]=b[i].split("\\d. ")[1];

    for(int i=0;i<b.length;i++)
      l=l.replace(b[i], "<b>"+b[i]+"</b>");
Emil