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=\""}], "\""])];
Map[StringCases[#, Shortest["|uniprotkb:" ~~ aa__ ~~ "(gene name)"] -> aa, Overlaps -> True] &, test1]– HyperGroups May 27 '15 at 09:42htmlStringTrimdoes not give me the output that you show; instead I get:"<strong style=\"font-size:20px; color:#dfa57c\" style=\"font-size:20px; color:#dfa57c; \" class=\"new\">"– Mr.Wizard May 27 '15 at 13:09\\in StackExchange instr11, which will affect the function. Now I‘ve removed that. – HyperGroups May 27 '15 at 13:28htmlStringTrim? – Mr.Wizard May 27 '15 at 13:32stylewill be in a string, consideringInline style of css.– HyperGroups May 27 '15 at 14:06