0

I have text like

String str = "<x>abc</x>";

and want to match against

<x>
   abc
</x>

i tried to do something like:

str = str.replaceAll(">",">\\\\w*");
str = str.replaceAll("<","\\\\w*<");

Pattern pattern = Pattern.compile(str);

how the value of str in the compile is actually

\w*<x>\w*abc\w*</x>\w*

i was expecting

\\w*<x>\\w*abc\\w*</x>\\w*
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Vik
  • 8,721
  • 27
  • 83
  • 168
  • 1
    If you are working with backslashes in regex, keep in mind they can either escape something in the python string literal, or in the regular expression Itself. To get an actual backslash either use raw strings (preferred) `r"\\w*<"` Or double-escape them `"\\\\w*<"` – Felk Mar 08 '16 at 23:00
  • double escaping gives just one backslash rather two – Vik Mar 08 '16 at 23:08

1 Answers1

0

I have to use eight(8) \ characters (i.e. \\\\\\\\) in order to get two(2) in the output. It's a little strange but works.

William Price
  • 4,033
  • 1
  • 35
  • 54
Vik
  • 8,721
  • 27
  • 83
  • 168