New answers tagged

1

I came up with another way to do this: expr /. b[a_]*c_f :> Block[{$patternMatched}, With[{result = c /. (d[a] :> ($patternMatched = True; e))}, result /; $patternMatched]] Once the outer pattern matches (b[a_]*c_f), a boolean is set up $patternMatched. Then, if the inner pattern matches (d[a] appears inside c) then $patternMatched is ...


8

This works: Block[{Pick}, Thread@Pick[{1, 2}, {0, 1}, Except[0]]] This is due to the fact that Pick[1,0,Except[0]] also works and gives Sequence[]. It appears, the pattern is working first on the whole expression. As {0, 1} matches Except[0], it gives true and the corresponding element is the entire list {1, 2}. Another quick workaround is this: ...


3

Looks like Except doesn't work in Pick pattern matching. Here's a workaround. Pick[{1, 2, 3, 4}, {0, 1, 0, 2}, _?(# != 0 &)] {2, 4} Or similar to the form in Pick: Possible Issues (last example) list = {1, 2, 3, 4}; selector = {0, 1, 0, 2}; newSelector = Map[MatchQ[#, Except[0]] &, {0, 1, 0, 2}]; Pick[list, newSelector] {2, 4}


10

To answer the question of why there is no match let's look at the output of Trace: Cases[{"key" -> Association[]}, HoldPattern["key" -> Association[]]] // Trace (* {{{{Association[], <||>}, "key" -> <||>, "key" -> <||>}, {"key" -> <||>}}, Cases[{"key" -> <||>}, HoldPattern["key" -> Association[]]], {}} ...


5

Clarification of HoldPattern usage: HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form. I think this can be ambigious for begginers. One could think that what we are doing in MatchQ[HoldPattern[a[1]], HoldPattern[_[_]]] is to more or less MatchQ[ a[1], _[_] ] where both arguments are kept ...


4

The following is an expansion of the explanation given by Mr.Wizard. The pattern-matcher works on the base of the assumption that Orderless attribute is already applied and the arguments of the Orderless function are already sorted in the canonical order: ClearAll[o] SetAttributes[o, Orderless] MatchQ[Hold[o[y, x, a]], Hold[o[_, x, a]]] (* unsorted ...


4

UPDATE That question is by the essence an exact duplicate of this one. The explanation given by Mr.Wizard means that the pattern-matcher is NOT capable to handle situations when an unevaluated function with Orderless attribute is wrapped by Hold. So this is indeed a gedanken functionality. The pattern-matcher works on the base of the assumption that ...


3

You can put a Verbatim on the Plus: MatchQ[Hold[x + 2 y + 0], Hold[Verbatim[Plus][x, 2 _, 0]]] (* True *) Another way: expr = Inactivate[x + 2 y + 1]; form = Inactivate[x + 2 _ + 1]; MatchQ[expr, IgnoringInactive@form] (* True *)


7

Under the Possible Issues tab of the Cases documentation. Use HoldPattern to treat the rule itself as a pattern: Cases[{a -> aa, b -> bb, c -> cc, d -> dd, e -> ee}, HoldPattern[e -> ee]] (*{e -> ee}*)


1

Select[{a -> aa, b -> bb, c -> cc, d -> dd, e -> ee} , MatchQ[#, Rule[e, Blank[]]] &] returns {e->ee} To remove all patterns involving e->_ use Complement[#, Select[#, MatchQ[#, Rule[e, Blank[]]] &]] & @ {a -> aa, b -> bb, c -> cc, d -> dd, e -> ee} Bingo! After learning about HoldPattern, ...


1

expr = x^α (9/(100 α κ Gamma[α]) - x/(100 (α + α^2) κ Gamma[α]) - Subscript[a, 0]/(α Gamma[α])) + x^(2 α) ((2^(2 - 2 α) Cos[π α] Gamma[ 1/2 - α] Subscript[a, 0])/(5 Sqrt[π] α κ Gamma[α]) - (x \ Subscript[a, 0])/(10 κ Gamma[2 + 2 α]) - (x Gamma[ 2 + α] Subscript[a, 0])/(10 α κ Gamma[α] Gamma[ ...


7

With both StringPattern and RegularExpression the problem is greediness: wildcards will try to match as much as possible. With StringPattern this can be fixed using Shortest: StringReplace[buf, "\\text{" ~~ Shortest[x___] ~~ "}" :> x] With a regular expression a quantifier can be made ungreed with ? (e.g. {(.*?)}), but when you're going that way, you ...



Top 50 recent answers are included