Questions tagged [pattern-matching]

Use this tag for questions about testing whether a data structure has a particular shape or contains particular values in certain locations. Many functional languages provide pattern matching constructs. Most questions in this tag should also have the tag for the language you are programming in. DO NOT USE THIS TAG FOR REGULAR EXPRESSION QUESTIONS, USE [regex] INSTEAD; similarly, for pattern matching (globbing) in POSIX-like shells, use [glob].

What is pattern matching?

Pattern matching means checking whether a data structure conforms to a certain pattern. In the abstract, a pattern can be any set of values; however most languages restrict patterns to expressing structural constraints, such as “all lists with at least two elements” or “all 2x2 matrices where the elements at (1,0) and (0,1) are equal”.

Languages such as ML, Haskell, Erlang and Mathematica have core language constructs for pattern matching. Other languages such as Lisp have derived pattern matching constructs.

Pattern-matching enhances a language in two directions: expressiveness, as complex sequences of tests can be written concisely, and performance, as the compiler is able (at least when pattern-matching is a core language construct) to optimize such constructs to generate the optimal number of tests (i.e. never checking twice the same condition).

Tag usage guidance

Regular expressions are an important special case of pattern matching on strings. Do not use the tag for regular expression questions, use the tag instead.

Pattern matching is normally exact. If you're looking for approximate patterns, for example in image or speech analysis, look for “recognition” rather than “matching”, for example .

8954 questions
765
votes
9 answers

Remove a fixed prefix/suffix from a string in Bash

I want to remove the prefix/suffix from a string. For example, given: string="hello-world" prefix="hell" suffix="ld" How do I get the following result? "o-wor"
Dušan Rychnovský
  • 11,699
  • 8
  • 41
  • 65
562
votes
3 answers

What is the difference between "x is null" and "x == null"?

In C# 7, we can use if (x is null) return; instead of if (x == null) return; Are there any advantages to using the new way (former example) over the old way? Are the semantics any different? Is it just a matter of taste? If not, when should I use…
Maniero
  • 10,311
  • 6
  • 40
  • 85
382
votes
11 answers

How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?

Say I want to copy the contents of a directory excluding files and folders whose names contain the word 'Music'. cp [exclude-matches] *Music* /target_directory What should go in place of [exclude-matches] to accomplish this?
user4812
  • 5,942
  • 9
  • 30
  • 35
362
votes
14 answers

How can I tell if a string repeats itself in Python?

I'm looking for a way to test whether or not a given string repeats itself for the entire string or not. Examples: [ '0045662100456621004566210045662100456621', # '00456621' '0072992700729927007299270072992700729927', …
John
  • 15,418
  • 12
  • 44
  • 65
271
votes
15 answers

Count character occurrences in a string in C++

How can I count the number of "_" in a string like "bla_bla_blabla_bla"?
andre de boer
  • 3,124
  • 3
  • 17
  • 15
240
votes
4 answers

What does `:_*` (colon underscore star) do in Scala?

I have the following piece of code from this question: def addChild(n: Node, newChild: Node) = n match { case Elem(prefix, label, attribs, scope, child @ _*) => Elem(prefix, label, attribs, scope, child ++ newChild : _*) case _ => error("Can…
amorfis
  • 15,390
  • 15
  • 77
  • 125
178
votes
12 answers

Javascript fuzzy search that makes sense

I'm looking for a fuzzy search JavaScript library to filter an array. I've tried using fuzzyset.js and fuse.js, but the results are terrible (there are demos you can try on the linked pages). After doing some reading on Levenshtein distance, it…
willlma
  • 7,353
  • 2
  • 30
  • 45
167
votes
5 answers

Using comparison operators in Scala's pattern matching system

Is it possible to match on a comparison using the pattern matching system in Scala? For example: a match { case 10 => println("ten") case _ > 10 => println("greater than ten") case _ => println("less than ten") } The second case…
BefittingTheorem
  • 10,459
  • 15
  • 69
  • 96
159
votes
10 answers

How to select lines between two marker patterns which may occur multiple times with awk/sed

Using awk or sed how can I select lines which are occurring between two different marker patterns? There may be multiple sections marked with these patterns. For example: Suppose the file contains:…
dvai
  • 1,953
  • 3
  • 13
  • 15
156
votes
5 answers

Ant path style patterns

What are the rules for Ant path style patterns. The Ant site itself is surprisingly uninformative.
MDK
  • 1,631
  • 3
  • 12
  • 5
152
votes
8 answers

PostgreSQL LIKE query performance variations

I have been seeing quite a large variation in response times regarding LIKE queries to a particular table in my database. Sometimes I will get results within 200-400 ms (very acceptable) but other times it might take as much as 30 seconds to return…
Jason
  • 2,259
  • 2
  • 17
  • 12
150
votes
10 answers

What is 'Pattern Matching' in functional languages?

I'm reading about functional programming and I've noticed that Pattern Matching is mentioned in many articles as one of the core features of functional languages. Can someone explain for a Java/C++/JavaScript developer what does it mean?
Roman
  • 64,384
  • 92
  • 238
  • 332
147
votes
3 answers

Does PostgreSQL support "accent insensitive" collations?

In Microsoft SQL Server, it's possible to specify an "accent insensitive" collation (for a database, table or column), which means that it's possible for a query like SELECT * FROM users WHERE name LIKE 'João' to find a row with a Joao name. I know…
Daniel Serodio
  • 4,229
  • 5
  • 37
  • 33
139
votes
5 answers

Ruby Regexp group matching, assign variables on 1 line

I'm currently trying to rexp a string into multiple variables. Example string: ryan_string = "RyanOnRails: This is a test" I've matched it with this regexp, with 3 groups: ryan_group = ryan_string.scan(/(^.*)(:)(.*)/i) Now to access each group I…
ryanjones
  • 5,383
  • 4
  • 28
  • 24
129
votes
4 answers

How is pattern matching in Scala implemented at the bytecode level?

How is pattern matching in Scala implemented at the bytecode level? Is it like a series of if (x instanceof Foo) constructs, or something else? What are its performance implications? For example, given the following code (from Scala By Example pages…
Esko Luontola
  • 73,184
  • 17
  • 117
  • 128
1
2 3
99 100