0
$\begingroup$

This is a problem related to How to use StringReplace "from bottom to global"?

The answer of @J.M. works well for "<...>" styles :

StringReplace["erewrer<.rer>r33e<erer>re<><>rer\nrer<ew212>", 
          Shortest["<" ~~ __ ~~ ">"] -> ""]
"erewrerr33ererer\nrer" 

Now suppose I have a string

strinput="It will only take a moment/minute/second"

I want to get

 stroutput="It will only take a second"

So I tried

StringReplace[#, {Shortest[" " ~~ ___ ~~ "/"] -> 
 " "}] &@"It will only take a moment/minute/second"

"It minute/second"

Strangely the whole "will only take a moment/" was eaten. Why this happen? What should I do?

$\endgroup$
2
  • 3
    $\begingroup$ Try {" " ~~ Except[" "] .. ~~ "/" -> " "} and take a look here: 72283 $\endgroup$
    – Kuba
    Jun 29, 2016 at 8:03
  • 2
    $\begingroup$ For things like this, tag the pattern and print, e.g. StringReplace[#, {xx : Shortest[" " ~~ ___ ~~ "/"] :> (Print[xx]; " ")}] &@"It will only take a moment/minute/second" and you can see exactly what's matched (and that it's doing what you asked it to in this case). $\endgroup$
    – ciao
    Jun 29, 2016 at 8:06

0

Browse other questions tagged or ask your own question.