Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to delete the string representing the context of a symbol. For info I need such a functionality in my ShowIt function defined here

I'd like to know how I can replace such a string with a rule so that for example,

"MyFunc[ShowIt[Q1`Private`var1$123], ShowIt[Q2`Private`var2$456], 0., 1.]"

gives

"MyFunc[ShowIt[var1], ShowIt[var2], 0., 1.]"

or

"Q1`Private`var1$123"

gives

"var1"
share|improve this question

2 Answers 2

up vote 4 down vote accepted

Trying to detect and replace strings that represent symbols is problematic. Essentially you need to parse the string as an expression to be robust, and that it better done with built-in parsing than with StringReplace.

Documentation for SymbolName says:

SymbolName always gives the short name, without any context.

Therefore you may use:

short =
  Module[{T, SR = StringReplace[#, a__ ~~ "$" ~~ DigitCharacter .. :> a] &},
    With[{ME = MakeExpression, U = Unevaluated},
     ME @ #
       /. s_Symbol :> T @ ME @ SR @ SymbolName @ U @ s
       /. T@_@x_   :> x
       /. _@x_     :> U@x ~ToString~ InputForm
  ]] &;

short @ "MyFunc[ShowIt[Q1`Private`var1$123], ShowIt[Q2`Private`var2$456], 0., 1.]"
"MyFunc[ShowIt[var1], ShowIt[var2], 0., 1.]"
short @ "MyFunc[ShowIt[\"Not`a`symbol\"], ShowIt[Q2`Private`var2$456], 0., 1.]"
"MyFunc[ShowIt[\"Not`a`symbol\"], ShowIt[var2], 0., 1.]"

Of course the string may change somewhat because of the parse and string conversion, much as doing Cell > Convert To... (form) does, but if this string is already automatically generated that should not be a problem.


Now that I have seen your application inside ShowIt it seems clear that you should avoid the conversion to a String in the first place.

Where you are using:

ExtactSymbolName[str_String]:=
    With[{pat = WordCharacter},
        StringReplace[str, (pat ... ~~ "`") .. ~~ s : pat .. ~~ ("$" ~~ NumberString) ... :> s]
    ];

ToExpression[ ExtactSymbolName @ ToString @ Unevaluated @ expr, InputForm, Defer]

I would use:

Module[{T, SR = StringReplace[#, a__ ~~ "$" ~~ DigitCharacter .. :> a] &},
  Defer[expr]
    /. s_Symbol :> T @ MakeExpression @ SR @ SymbolName @ Unevaluated @ s
    /. T@_@x___ :> x
]
share|improve this answer
    
Thank Mr. Wizard I'll have a curious look at you answer after sleeping. –  Faysal Aberkane Aug 5 '12 at 3:31
    
Hmmm. I was looking for SymbolName about 2 months ago. :P –  rcollyer Aug 20 '13 at 13:49
    
@rcollyer Why didn't you ask the Great and Powerful Wizard? :^) (But don't be disappointed by the man behind the curtain.) –  Mr.Wizard Aug 20 '13 at 15:00
    
My method was a bit more crude, as I was converting to a string representation, but it was effective. As to looking behind the curtain, I prefer to be invited, first, otherwise it is just impolite. :) –  rcollyer Aug 20 '13 at 15:09

I wrote something very similar to what you want in this answer. Modifying the core of the answer there to also handle temporary variables from Module, you can do:

str = "MyFunc[ShowIt[Q1`Private`var1$123],ShowIt[Q2`Private`var2$456],0., 1.]";    
With[{pat = WordCharacter},
    StringReplace[str, (pat ... ~~ "`") .. ~~ s : pat .. ~~ ("$" ~~ NumberString) ... :> s]
]

(* "MyFunc[ShowIt[var1],ShowIt[var2],0.,1.]" *)
share|improve this answer
    
Great thank you very much –  Faysal Aberkane Aug 3 '12 at 15:44
    
(LetterCharacter | DigitCharacter) is just the same as WordCharacter, no? –  J. M. Aug 3 '12 at 22:12
    
yup $\phantom{}$ –  rm -rf Aug 3 '12 at 23:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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