Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. ClearAll[foo, labeledFoo];
  2.  
  3. labeledFoo = {"FooBarBazQuux", foo};
  4.  
  5. labeledFoo /. hdr_String :>
  6. StringReplace[hdr, l_?LowerCaseQ ~~ U_?UpperCaseQ :> l <> " " <> U]
  7.  
  8. (* {"Foo Bar Baz Quux", foo} *)
  9.  
  10. labeledFoo /. hdr_String :>
  11. StringReplace[hdr, l_?LowerCaseQ ~~ U_?UpperCaseQ :> l <> " " <> U] /.
  12. {hdr_String, x_} :> (hdr -> x)
  13.  
  14. (* "Foo Bar Baz Quux" -> foo *)
  15.  
  16. labeledFoo /. {hdr_String, x_} :> (Rule @@ {StringReplace[hdr,
  17. l_?LowerCaseQ ~~ U_?UpperCaseQ :> l <> " " <> U], x})
  18.  
  19. (* "Foo Bar Baz Quux" -> foo *)
  20.  
  21. labeledFoo /. {hdr_String, x_} :> (StringReplace[hdr,
  22. l_?LowerCaseQ ~~ U_?UpperCaseQ :> l <> " " <> U] -> x)
  23.  
  24. (* "Fo" ~~ l <> " " <> U ~~ "a" ~~ l <> " " <> U ~~ "a" ~~
  25. l <> " " <> U ~~ "uux" -> foo *)
  26.  
  27. StringReplace["FooBarBazQuux",l$_?LowerCaseQ~~U$_?UpperCaseQ:>l<>" "<>U]
  28.  
  29. {{1, 2}, 3} /. {x_List, y_Integer} :>
  30. Rule[
  31. Replace[x, {l_Integer, u_Integer} :> l + u],
  32. y
  33. ]
  34.  
  35. (* l + u -> 3 *)
  36.  
  37. {x_, x+1} -> x^2
  38.  
  39. {{1, 2}, 3} /. {x_List, y_Integer} :>
  40. Block[{yy = y},
  41. Rule[
  42. Replace[x, {l_Integer, u_Integer} :> l + u],
  43. yy]
  44. ]
  45.  
  46. (* 3 -> 3 *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement