-1

I have a some strings where I need to replace \L and \l where there is a starting \L and ending \l Example:

Some Text \LThis is \\L What I want \\l and this\l Some more Text

I need to take into account where \L or \l can be double escaped to (\L or \l) to actually mean \L.

I want the above to come out as:

Some Text This is \L What I want \l and this Some more Text

I think I've managed to solve it:

    String st = "Some Text \\LThis is \\\\L What I want \\\\l and this\\l Some more Text";

    String newstring = st.replaceAll("(\\\\L)(.*)(\\\\l)", "$2");
  • Does this answer your question? [Java Regex Capturing Groups](https://stackoverflow.com/questions/17969436/java-regex-capturing-groups) – Franck May 13 '22 at 20:39
  • You can test your regex against your text at https://regexr.com/ `(\\L.*\\l)` looks like a starting point. – Franck May 13 '22 at 20:41
  • If you tried any code or pattern, it would be great if you posted that, because we do not know where you are stuck. – Wiktor Stribiżew May 13 '22 at 20:45
  • @Franck yeah been on regexer.com with the above, I could sort of capture the groups but I capture everything with .* – Monkmachine May 13 '22 at 20:48
  • @Monkmachine You could do first a replacement and then the regex. Or use the solution from Nik Owa. – Franck May 13 '22 at 20:54

0 Answers0