Search code examples
javaregexregular-language

How to exclude all subsequences of string from a long string in Java


How can I write code in Java using Regex and ReplaceAll method which would exclude all sub sequences from long string:

:' goes to '
:+ goes to +
+' goes to '

For example, if i have:

string U:'0 that after reduction be U'0
string U+'0 that after reduction be U'0

If I have a long string, the operation should be repeated until all combinations of those three operations no longer changes the string.

I am not so good with regular expressions, so I would appreciate any help.

Thanks


Solution

  • I'm not sure that regular expressions are the correct tool for this. Is there any reason why you can't just do:

    String myString = "This U:'0 is a U+'0 string."
    myString = myString.Replace(":'", "'");
    myString = myString.Replace(":+", "+");
    myString = myString.Replace("+'", "'");
    

    Unless the logic behind the transformations is far more involved than you are actually suggesting, there's no reason why this wouldn't be sufficient.