Why is
"Xabcde" /. "X" ~~ e__ -> e
"Xabcde"
and not
"abcde"
Why is
"Xabcde" /. "X" ~~ e__ -> e
"Xabcde"
and not
"abcde"
Use
StringReplace["Xabcde", "X" ~~ e__ -> e].
Replace, et al are for lists/expressions...
Notice that AtomQ@"Xabcde" is True, so regular (non-string) replace operations only "see" it as a singular entity:
"Xabcde" /. "Xabcde" -> 1
(* 1 *)
From the docs for ReplaceAll: "... to transform each subpart..." - but there is no "subpart" for atoms, so regular replace operations only operate on the string as a complete entity.
If you want to do such things as part of a larger replacement program, something like this can be done:
test = {"Xabcde", {1, 2, 3}};
test /. {a_String :> StringReplace[a, "X" ~~ e__ -> e], {a_, b_, c_} :> {b, c}}
(* {"abcde", {2,3}} *)
/. to achieve string pattern matching and replacement.
– orome
Apr 04 '15 at 22:18
StringReplace is for - perhaps I'm being dense and not understanding your question - but see update, perhaps that's your intent?
– ciao
Apr 04 '15 at 22:22
/.. The other was to understand why my pattern wasn't working. I'm also curious how is have figured this out from the docs.
– orome
Apr 04 '15 at 22:26
"Xabcde" /. a_ :> StringCases[a, _] /. {"X", b__} :> StringJoin[b]
(*"abcde"*)