# Pattern match and then replace the argument

I've want to replace the pattern, say I the arbitrary function $$o$$ that takes one argument, now, if the two of these function meet in a expression, one with some arugment $$B$$ and the other with the conjugated argument $$B^\dagger$$ I want to replace these into one function called $$k$$ but with the twist, that the argument is now the same letter but lower case symbolically

$$o(B) o(B^\dagger) \rightarrow k(b)$$ What I thought would work, althought it does not

o[-B] o[-B\[Dagger]] /. o[A__] o[A\[Dagger]__] -> (k[ToExpression@ToLowerCase[ToString[A]]]

sadly this doesn't work. Anyone know the solution?

Your problem comes from the fact that MMA considers "A[Dagger]" a single symbol different from "A". To match you need therefore 2 different patterns (note, do not use capital letters, they are reserved for system symbols):

o[-x_] o[-y_]

The second problems comes from ensuring that you have the same symbol as without dagger. You first need to extract the symbol name (a string), get rid of the dagger, store it in tmp for later use, transform it into a symbol and compare with the the first pattern

o[-x_] o[-y_] /; (Symbol[ tmp = StringTake[ToString[SymbolName[y]], 1]] == x )

The next problem is the creation of a new symbol with a lower case name. As we already have the string with upper case, we simply need "ToLowerCase" to create a new symbol:

Symbol[ToLowerCase[tmp]]

Everything together:

o[-B] o[-B\[Dagger]] /.  o[-x_] o[-y_] /;
(Symbol[tmp = StringTake[ToString[SymbolName[y]], 1]] == x ) :>
k[Symbol[ToLowerCase[tmp]]]

k[b]

Try

o[ B] o[Conjugate[B]] /. o[A_ ] o[Conjugate[A_]] -> k [A]
(* k[B]*)
• But I want the B to be lower case.... Apr 18 at 11:52
• Replace Rule (->) by RuleDelayed (:>)
– Acus
Apr 18 at 12:31
• A dagger is ConjugateTranspose Apr 18 at 13:05