1
$\begingroup$

A problem about StringReplace


When I'm running into this problem possible [bug]

str11 = "<strong style=\"font-size:20px\" style=\"color:#dfa57c\" 

style=\"font-size:20px\" style=\"color:#dfa57c\" class=\"new\">";

StringCases[str11, 

 Repeated[Shortest["style=\"" ~~ ___], {2, 10}] ~~ "\""]

(*
    {style="font-size:20px" style="color:#dfa57c",style="font-size:20px" style="color:#dfa57c"
}
*)

There is a problem of the above code, there are two result, how can I get just one Max repeated result? Something like the effect of Longest

Here is like the following result.

StringCases[str11, Repeated[Shortest["style=\"" ~~ ___], {4}] ~~ "\""]

(*
    {style="font-size:20px" style="color:#dfa57c" style="font-size:20px" style="color:#dfa57c"
}
*)

StringReplace[str11, 

 x : Repeated[Shortest["style=\"" ~~ ___], {2, 20}] ~~ "\""]

(*
    <strong ="font-size:20px" style="color:#dfa57c ="font-size:20px" style="color:#dfa57c clas
s="new">
*)

StringReplace[str11, 

 p : Repeated[Shortest["style=\"" ~~ ___], {4}] ~~ "\"" :> 

  "style=" <> 

   StringReplace[

    StringJoin[

Riffle[StringSplit[

  StringReplace[p, "style=" -> ""], {"style=", " "}], 

 ";", {2, -1, 2}]], "\";\"" -> "; "] <> "\""]

(*
    <strong style="font-size:20px; color:#dfa57c; font-size:20px; color:#dfa57c;" class="new">
*)

Question: Can the count be removed? Are there any simpler solutions?

htmlStringTrim[x_] := (count = StringCount[x, "style"]; 

  StringReplace[x, 

   p : (Repeated[Shortest["style=\"" ~~ ___], {count}]) ~~ "\"" :> 

    StringJoin[

 Riffle[StringSplit[p, {"\" style=\""}], "; ", {2, -1, 2}]] <> 

"\""])

htmlStringTrim[str11]

(*
    <strong style="font-size:20px; color:#dfa57c; font-size:20px; color:#dfa57c; " class="new"
>
*)

Thanks for Mr.Wizard's answer.

htmlStringTrimNew[x_] := 
  StringReplace[x, 
   p : (("style=\"" ~~ Except["\""] .. ~~ "\"" ~~ 
     Whitespace | "") ..) :> (
 "style=\"" <> 
  StringJoin[
   StringInsert[StringReplace[#, "\"" :> ""], ";", -2] & /@ 
    StringSplit[p, {"style=\""}], "\""])];
$\endgroup$
  • 1
    $\begingroup$ I think this should answer your question, unless I missed the point which is possible here. - using the StringCase function and Shortest option $\endgroup$ – Kuba Apr 26 '15 at 13:28
  • $\begingroup$ HyperGroups do you agree with Kuba that your question is answered in the linked Q&A? $\endgroup$ – Mr.Wizard May 13 '15 at 9:13
  • $\begingroup$ @Mr.Wizard I don't agree that, the most confused problem of the question is how to match the repeated pattern. that problem is matching pattern from left to right something like Map[StringCases[#, Shortest["|uniprotkb:" ~~ aa__ ~~ "(gene name)"] -> aa, Overlaps -> True] &, test1] $\endgroup$ – HyperGroups May 27 '15 at 9:42
  • $\begingroup$ @HyperGroups could you reduce the question to the unique problem. It is so large now it discourages from reading, imo. $\endgroup$ – Kuba May 27 '15 at 10:28
  • $\begingroup$ @Kuba how about now $\endgroup$ – HyperGroups May 27 '15 at 11:09
1
$\begingroup$

I think this matches what you want it to:

str11 = "<strong style=\"font-size:20px\" style=\"color:#dfa57c\" 
  style=\"font-size:20px\" style=\"color:#dfa57c\" class=\"new\">";

StringCases[str11, ("style=\"" ~~ Except["\""] .. ~~ "\"" ~~ Whitespace | "") ..]
{"style=\"font-size:20px\" style=\"color:#dfa57c\" 
 style=\"font-size:20px\" style=\"color:#dfa57c\" "}
$\endgroup$
  • $\begingroup$ nice, the use of Except is important where I should make progress. $\endgroup$ – HyperGroups May 27 '15 at 13:57

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.