Questions tagged [pattern-matching]

Questions on expression testing and manipulation through pattern matching and constructing efficient patterns.

Filter by
Sorted by
Tagged with
3
votes
1answer
82 views

DeleteCases with a case as float?

Might the DeleteCases have issues in matching when the list is of floats (machine precision numbers)? E.g. in this code it might fail to match correctly because ...
1
vote
0answers
29 views

Apply SequenceReplace with a fixed shift

How can I apply SequenceReplace with a fixed prespecified shift instead of a shift equal to the length of the pattern, on a given list? E.g. ...
6
votes
5answers
170 views

Taking some terms from a polynomial

I have a polynomial like this: ...
3
votes
1answer
47 views

Naming repeated patterns

How can I name a repeated pattern to apply a rule on it? In the example below I have two element sequence. There is no problem in naming them and then applying some function on it: ...
7
votes
2answers
80 views

How to use ExtractArchive[] with a pattern that does NOT match

ExtractArchive has the ability to Extract all files that match a pattern. For example this will extract all the JPEG files from the archive: ...
3
votes
2answers
54 views

Setting nested optional argument with a default when unpacking from a given Head

I have a head that holds a single argument, and I want to unpack the value inside it while also assigning a symbol to the value itself, for example: ...
4
votes
2answers
118 views

How to apply Cases on multiple parts of sublists?

I have the following data sol={{2063, 853}, {2066, 855}, {2069, 857}, {2072, 859}} I can find prime-pairs by ...
4
votes
3answers
94 views

Extract the name of a point with Regular Expression

I have a list of points as strings in the form {"A1=(345.2345,3423.1)","B=(2123,97.123)","KX=(2144.546,-4455)"} and I would like to extract the names using ...
3
votes
3answers
71 views

Adding values of an association through pattern matching

I have an Association in which values are lists of lists containing an Integer and a DateObject. I would like to build a function that adds all Integers that have the same dates. ...
5
votes
4answers
95 views

Grouping and repeating the grouped rows inside a matrix

I'm interested in finding the quickest way of grouping the rows of a large matrix in sub-groups based on the position of non-zero elements in the first column and then repeating them number of times ...
5
votes
1answer
115 views

Generate pattern held by HoldPattern

I am trying to match expressions that have factors with certain exponents. For instance, I would like to match as follows ...
1
vote
1answer
33 views

How to modify a string respecting line breaks and line beginning?

I'm trying to add quotation marks to the elements of a list which happens to be a single string separated by commas and line breaks, to then split the string at the commas respecting line breaks. ...
1
vote
1answer
55 views

Map patterns to values

Suppose I have a list of named patterns: listofPatterns = {a_Integer, b_Real, c_?StringQ} I would like to have a mapping from a pattern to a certain value like: ...
5
votes
3answers
404 views

ReplaceAll (/.) not working as expected

I am trying to replace some variables in the form of n*wt with wt. However, I came across some cases where the ...
0
votes
0answers
41 views

How to make a Flat function return x when evaluated as f[x]?

With SetAttributes[f, Flat], f[a_] can get matched to f of any number arguments. How can I ...
3
votes
2answers
181 views

What is the correct pattern to DeleteCases from a list?

I'm trying to get better with pattern matching but even after years of using MMA it's still a murky concept. The docs are actually pretty good in this regard but I still struggle. In this situation I ...
1
vote
0answers
52 views

Difference between Plus[x_, y_] and Plus[_, _] [duplicate]

Why did MatchQ[a + b + c, Plus[x_, y_] is considered true? In my opinion, it should only consider 2 elements within Plus, ...
2
votes
2answers
39 views

Pattern matching on a string, conditional to an underlying data structure

Apologies for the complex title. I run into one of these unexpected definition features. I have a simplified example of the problem: a simple parser that uses pattern matching to check if categories ...
1
vote
0answers
35 views

Pattern matching and searching for subgraphs?

I have a large list of graphs where each node has a dictionary. I'd like to query that list like a database, ie. I have a graph and want to find examples that contain it as a subgraph (and where the ...
1
vote
2answers
95 views

Patterns with AND, OR, NOT

I pretend to use DictionaryLookup[{"Language", complex_Pattern}] but can not achieve it finally to get it with like three layers ... Specially, I tried ...
0
votes
0answers
34 views

Brackets on pattern test with argument

I am defining a pattern to be used as a test, that itself depends on an argument. For instance, I might want to check that something appears in a given list: ...
4
votes
1answer
85 views

Pattern matching natural powers as products

I thought that Mathematica would be smart enough to know that f[t]^2 should match a_ * b_ with ...
4
votes
2answers
57 views

String Cases Pattern Questions

Say I have the function: dep = TextStructure["He wrote a book. I read the book he wrote.", "DependencyStrings", PerformanceGoal -> "Speed"] Which outputs: ...
1
vote
1answer
40 views

How to deal with procedurally generated rules and patterns?

I'm trying to procedurally generate replacement rules of the following form ...
3
votes
2answers
86 views

How does mathematica decide what `x_` is in `MemberQ[list,x_/;(x==#)]&/@list`

Consider the following code: list={1,2} MemberQ[list, x_ /; (x == #)] & /@ list This gives {True,True} ...
1
vote
1answer
44 views
9
votes
3answers
217 views

How to define a Rule to match a Head except if it is a Part of another?

I have an expr which has List at various levels and I want to display them nicely as a column inside a frame but my problem is ...
10
votes
3answers
310 views

StringContainsQ, but anywhere in order

I am trying to find a string within a string, but "anywhere", so long as it is in the right order. For example StringContainsQ["aabbc","ac"] returns ...
2
votes
1answer
28 views

StringMatchQ with Alternatives

Why when trying to find whether one (out of several) patterns matches an expression - using StringMatchQ - all of them seem to be ignored and no matches are found? ...
3
votes
1answer
48 views

How can I identify symbols within an expression which are wrapped in a local HoldForm?

Let's assume a=b, that is, a has value b. In a given expression like e.g. a + i + HoldForm[a + k] + l I would like to ...
2
votes
1answer
48 views

Store boolean expression into a variable

My purpose is to use a boolean expression as a requirement that a pattern must comply. The requirement is c1 == "a", and I am using it to select a string from a ...
7
votes
1answer
141 views

Are Hold attributes Mathematica's way to implement lazy pattern matching à la Haskell?

I'm reading this and I'm having a hard time understanding lazy patterns; in particular I'm baffled with the server-client toy problem and why there is a 'problem' of matching "...too early". The ...
8
votes
2answers
323 views

StringReplace with multiple patterns

I am experiencing what seems to be a strange behaviour with StringReplace while trying to add an extra pattern to an existing list of patterns. The problem is to translate a string into Morse code, ...
2
votes
2answers
246 views

Using regular expressions to replace constants by functions

I have an expression of the form: expression = A1*x + A2*x^2 + ... and I would like to replace A1 by ...
5
votes
1answer
99 views

How to apply assumptions to pattern matching?

I'm trying to remove terms whose exponents are positive in a polynomial. I tried this one; Assuming[t > 0, x^t /. {x^a_ /; Positive@a -> 0}] but the result ...
9
votes
2answers
327 views

Partial monomial pattern matching

Is there an elegant way to "partially" match monomials in a polynomial expression? Let me explain what I mean by "partially" with an example. Suppose I have ...
2
votes
1answer
87 views

Prevent an expression from evaluating a certain way (pattern-matching?)

I've been trying to write some simple code to let me use physics vector notation: $\hat{x}$, $\hat{y}$, $\hat{r}$, and $\hat{\theta}$. ...
2
votes
0answers
41 views

KeyUnion by equivalent patterns

I have associations whose keys are patterns. For example, ass1 = <|a_ -> 1, f[] -> 3|>; ass2 = <|b_ -> 2|>; I would like to construct the ...
5
votes
1answer
55 views

FindMoleculeSubstructure fails on SMARTS MoleculePattern, but MoleculePlot is OK?

Background Info: SMARTS is a convention for specifying molecular patterns. It is supported by Mathematica 12's MoleculePattern function. Problem Setup: Consider ...
2
votes
1answer
41 views
4
votes
2answers
73 views

Extracting values from function/list containing arguments/elements (using patterns)

Here's an example input list: ...
5
votes
2answers
244 views

How to insert a character between any several characters without knowing indexes?

There is a list of strings: {"x1x2", "x1", "x1x2x3", "x3", "x4", "x1x2x3x4"} The goal is to insert "&&" between every two elements in a list. I need to ...
6
votes
6answers
358 views

How to replace numbers by letters in a special way?

There are few sublists: lists = {{0,0,0}, {0,0,1}, {0,1,0}, {0,1,1}, {1,0,0}, {1,0,1}, {1,1,0}, {1,1,1}} The goal is to put A if the first element of a sublist ...
2
votes
2answers
92 views

Replace elements of a list if the conditional is false

There is a list: {1, {a, b, c}, 1, {1, 2, 3, 4, 5, 6, 7, 8}, {A, B, D, Z}} It contains sublists of different elements and several $1$s. The goal is to replace ...
19
votes
7answers
638 views

How to replace all elements of a list by a rule “element” -> “element_”

I have a list: {a, b, c, d, e, f, g, h, i, j} The goal is to get: {a_, b_, c_, d_, e_, f_, g_, h_, i_, j_} I can replace ...
6
votes
2answers
174 views

How to divide list according to the pattern?

There is a list of lists: {{0,0,0}, {0,0,1}, {0,1,0}, {0,1,1}, {1,0,0}, {1,0,1}, {1,1,0}, {1,1,1}} I need to regrup it's elements according to a pattern: "if the ...
2
votes
0answers
41 views

Does passing patterns between packages alter their name unbeknownst to the user? [duplicate]

This question is a partial followup to my previous question. Mainly, after using With to inject the proper argument in the function at the rhs of ...
2
votes
0answers
43 views

How to find consecutive things in nested expressions?

To find things that are consecutive you can use SequenceCases: ...
4
votes
1answer
146 views

Pattern matching expressions that have been simplified

I run into this type of issue frequently when trying to apply known identities to complicated expressions: some of my expressions will have their forms simplified by mathematica in such a way that ...
2
votes
1answer
77 views

How can Cases detect x as Times[1,x]

I'm trying to use Cases to extract the coefficients of a polynomial $$x^5+6x^4+2x^3-8x^2+x+10$$ I was hoping I can first find the coefficients of the $x,\,x^2,\,...,...

1
2 3 4 5
22