Tag Info

Hot answers tagged pattern-matching

15

SequenceReplace SequenceReplace[ p:{a_, 0..} :> Sequence @@ (p /. 0 -> a)] @ l {-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1} or FixedPoint[SequenceReplace[{a_, 0} :> Sequence[a, a]], l] {-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1} ReplaceRepeated An alternative way to use ReplaceRepeated with a single replacement rule: l //. ...

9

Define a simple function, using FoldList op = FoldList[If[#2 == 0, #1, #2] &]; l = {-1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1}; op@l (* {-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1} *)

7

Here is a differential equations/operator approach, leading to mathematics-based solutions instead of expression-matching ones. As the OP observes, matching algebraic expressions with expression patterns can be tricky, despite Mathematica's Optional patterns and Default values. For trigonometric functions it is usually trickier since, as @alx observes, ...

7

Perhaps something like this will appeal l //. {{a___, 1, 0, b___} -> {a, 1, 1, b}, {a___, -1, 0, b___} -> {a, -1, -1, b}}

5

Given: list = {-1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1} Then: Module[{prev = 0}, Replace[list, {0 :> prev, x_ :> (prev = x)}, {1}]] (* {-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1} *) The initial value assigned by prev = 0 is only used for lists that start with a zero -- choose another value if desired.

4

You can use TrigReduce to come to form with multiple angles instead of powers and then use MatchQ to compare the result with Sin[...] pattern. I also use here Inactive[Sin] to keep results of conversion Cos terms to corresponding Sin terms: MatchQ[(TrigReduce[expr]//Expand)/.{Sin[x_] -> Inactive[Sin][x], Cos[x_] -> Inactive[Sin][x + \[Pi]/2]}, a_. ...

3

Cases[{__Integer}] @ list {{1, 2}, {2, 3}, {3, 4}, {4, 5}} DeleteCases[{"#",___}] @ list {{1, 2}, {2, 3}, {3, 4}, {4, 5}} DeleteCases[{"#", ___}|{___,_Symbol,___}] @ list {{1, 2}, {2, 3}, {3, 4}, {4, 5}} Cases[Except[{"#" ,___}|{___,_Symbol,___}]] @ list {{1, 2}, {2, 3}, {3, 4}, {4, 5}}

3

A few more... l = {-1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1}; Do[If[l[[i]] == 0, l[[i]] = l[[i - 1]]], {i, 2, Length[l]}] l {-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1} fn[l_] := Block[{l1 = l}, Set[Part[l1, #[]], Part[l1, #[]]] & /@ SequencePosition[l, {_, 0}]; l1 ] l = {-1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1}; ...

2

StringCases[s, "<span>" ~~ Shortest[x___] ~~ "</span>" -> x] {"Data I want to extract", "More data I want to extract", "Even more data I want to extract"} Shortest >> Details: If no explicit Shortest or Longest is given, ordinary expression patterns are normally effectively assumed to be Shortest[p], while string patterns are ...

1

Here's an approach to match only the stuff inside the brackets (otherwise, it is equivalent to the approach in the question): Attributes[BalancedBracketPattern] = {HoldFirst}; Module[ {defName}, Quiet[ BalancedBracketPattern[name_Symbol: defName, left_: "{", right_: "}"] := Shortest[ left ~~ (name___) ~~ right /; StringCount[name, left] ==...

Only top voted, non community-wiki answers of a minimum length are eligible