1
$\begingroup$

Suppose I have an original string that looks like:

str1 = "erewrer<.rer>r33e<erer>re<><>rer\nrer<ew212>"

I want to get rid of all those discrete <...>s to get

"erewrerr33ererer\nrer"

I tried StringReplace

StringReplace[str1, "<" ~~ __ ~~ ">" -> ""]

but it produced

"erewrer"

which is the result with the outermost <...> removed.

What should I do?

$\endgroup$
4
$\begingroup$

You need to add Shortest[] to get the shortest consistent match(es), since Mathematica uses Longest[] (i.e. a greedy match) by default:

StringReplace["erewrer<.rer>r33e<erer>re<><>rer\nrer<ew212>", 
              Shortest["<" ~~ __ ~~ ">"] -> ""]
   "erewrerr33ererer\nrer"
$\endgroup$
  • 1
    $\begingroup$ Regex version: StringReplace[str1, RegularExpression["<(?s).+?>"] -> ""] $\endgroup$ – J. M. is in limbo Feb 23 '16 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.