question
conversion1 : Convert "[AA]AB[AA]BB[AA]B[AA]CCC" to "[A1A2]A3B1[A4A5]B2B3[A6A7]B4[A8A9]CCC"
conversion2 : Convert "[AA]AB[AA]BB[AA]B[AA]CCC" to "[A1A2]AB1[A3A4]B2B3[A5A6]B4[A7A8]CCC"
where A is sensitive to "[]"
In conversion 2, both A and B (even C, though no more B or C in "[]") could be sensitive to "[]", we can control them.
And we can ignore the string in "[]".
my tries
case1: String
sample1 = "[AA]AB[AA]BB[AA]B[AA]CCC"; pos = StringPosition[sample1, "A"];
StringReplacePart[sample1, ToString /@ pos, pos]
(* [{2, 2}{3, 3}]{5, 5}B[{8, 8}{9, 9}]BB[{14, 14}{15, 15}]B[{19, 19}{20, 20}]CCC *)
case2 List
sample2 = Characters @ sample1; pos = Position[sample2, "A"];
ReplacePart[sample2, Thread[Rule[List /@ pos, "A" <> # & /@ ToString /@ Range[Length @ pos]]]]
(* {[,A1,A2,],A3,B,[,A4,A5,],B,B,[,A6,A7,],B,[,A8,A9,],C,C,C} *)
How about the case with multiple patterns.
case 3
pos = Position[sample2, "A" | "B"];
This is not good
ReplacePart[sample2, Thread[Rule[List /@ pos, ToString /@ Range[Length @ pos]]]];
posList = Position[sample2, #] & /@ {"A", "B"};
good for A and B separately
ReplacePart[sample2, Thread[Rule[List /@ posList[[1]], "A" <> # & /@ ToString
/@ Range[Length @ posList[[1]]]]]]
(* {[,A1,A2,],A3,B,[,A4,A5,],B,B,[,A6,A7,],B,[,A8,A9,],C,C,C} *)
ReplacePart[sample2, Thread[Rule[List /@ posList[[2]], "B" <> # & /@ ToString
/@ Range[Length @ posList[[2]]]]]]
(* {[,A,A,],A,B1,[,A,A,],B2,B3,[,A,A,],B4,[,A,A,],C,C,C} *)
How to combine those to get the result
{[,A1,A2,],A3,B1,[,A4,A5,],B2,B3,[,A6,A7,],B4,[,A8,A9,],C,C,C}
or more elegant way to do this?
Either string or list case is needed, and both of them in aswer is better.
(* Input 10 ==< *)stuff - code in a code block will be understood to be input, it doesn't need extra signposts. – Simon Woods Jun 18 '13 at 08:37How to combine Input[11] and Input[12] to get the result. As well as the case: multiple output results are pictures which do not follow the input but in the end of the post being a total image. This is one prevous example. http://mathematica.stackexchange.com/a/26932/6648 – HyperGroups Jun 18 '13 at 09:19