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


I tried StringReplace

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

but it produced


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

What should I do?


1 Answer 1


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

              Shortest["<" ~~ __ ~~ ">"] -> ""]
  • 1
    $\begingroup$ Regex version: StringReplace[str1, RegularExpression["<(?s).+?>"] -> ""] $\endgroup$ Feb 23, 2016 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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