2
$\begingroup$

I have a global rule as follow:

transformRules =
 {a1 -> Subscript[Style["a", Italic], 1], 
  b1 -> Subscript[Style["b", Italic], 1], 
  c1 -> Subscript[Style["c", Italic], 1],
  d1 -> Subscript[Style["d", Italic], 1], 
  e1 -> Subscript[Style["e", Italic], 1], 
  f1 -> Subscript[Style["f", Italic], 1],
  a2 -> Subscript[Style["a", Italic], 2], 
  b2 -> Subscript[Style["b", Italic], 2], 
  c2 -> Subscript[Style["c", Italic], 2],
  d2 -> Subscript[Style["d", Italic], 2], 
  e2 -> Subscript[Style["e", Italic], 2], 
  f2 -> Subscript[Style["f", Italic], 2],
  A1 -> Subscript[Style["A", Italic], 1], 
  B1 -> Subscript[Style["B", Italic], 1],
  C1 -> Subscript[Style["C", Italic], 1], 
  D1 -> Subscript[Style["D", Italic], 1],
  E1 -> Subscript[Style["E", Italic], 1], 
  F1 -> Subscript[Style["F", Italic], 1],
  A2 -> Subscript[Style["A", Italic], 2], 
  B2 -> Subscript[Style["B", Italic], 2],
  C2 -> Subscript[Style["C", Italic], 2], 
  D2 -> Subscript[Style["D", Italic], 2],
  E2 -> Subscript[Style["E", Italic], 2], 
  F2 -> Subscript[Style["F", Italic], 2]};

It is tedious to write them one-by-one. So I write a function to transform then automatically.

Thanks for Kuba's hints

transform= # -> Subscript[Style[#1, Italic], #2] & @@ StringSplit[ToString[#], ""] &

transformRules =
 Function[
  {symbol},
   symbol ->
    Subscript[Style[#1, Italic], #2] & @@ StringSplit[ToString[symbol], ""]]

transformRules =
  transform /@ 
   {a1, b1, c1, d1, f1, a2, b2, c2, d2, e2, f2, A1, B1, C1, D1, E1, F1}

enter image description here

However, my auxiliary function transform cannot apply the following cases:

transform[AA1]
transform[AB12]

And I also know that the main problem is StringSplit[ToString[#], ""]

Question

This my first time to use the string function, and I search teh DOC about StringSplit. However, I cannot solve it.

  • How to split the string acoording the number?

For example,

A1-> {"A","1"}, AB1-> {"AB","1"}, AB12-> {"AB","12"},ABC12-> {"ABC","12"}

$\endgroup$
  • $\begingroup$ @Kuba, Yes, I make a mistake, I need the style _Symbol->Subscript[_String, _Integer] or _Symbol->Subscript[_String, _String] $\endgroup$ – xyz Jun 23 '15 at 9:22
6
$\begingroup$

I think you want something like one of these:

StringSplit[{"A1", "AB1", "AB12"}, d : DigitCharacter .. :> d]

StringSplit[{"A1", "AB1", "AB12"}, d__?DigitQ :> d]

Both return {{"A", "1"}, {"AB", "1"}, {"AB", "12"}}.

DigitCharacter should be faster as it converts to a regular expression internally whereas DigitQ does not:

StringPattern`PatternConvert /@ {DigitCharacter .., __?DigitQ} // Column
{"(?ms)\\d+", {}, {}, Hold[None]}

{"(?ms)(.+)(?C1)", {}, {{Hold[DigitQ], 1}}, Hold[None]}
$\endgroup$
  • $\begingroup$ Thanks very much. It is my first time to use the string function $\endgroup$ – xyz Jun 23 '15 at 9:31
3
$\begingroup$

Here is a formulation using regular expression groups and POSIX character classes.

transformRules = 
  (# -> First @
          StringCases[ToString[#], 
            RegularExpression["([[:alpha:]]+)([[:digit:]]+)"] :> 
              Subscript[Style["$1", Italic], ToExpression["$2"]]] &)

transformRules /@ {A1, AB1, AB12}

rules

The raw input form for the above is

{A1 -> Subscript[Style["A", FontSlant -> Italic], 1], 
 AB1 -> Subscript[Style["AB", FontSlant -> Italic], 1], 
 AB12 -> Subscript[Style["AB", FontSlant -> Italic], 12]}

Note that the 2nd argument of Subscript is an integer and not a string, which appears to be one of your requirements.

$\endgroup$
  • $\begingroup$ Thanks a lot for your solution:-) $\endgroup$ – xyz Jun 24 '15 at 1:45

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.