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
129
votes
2 answers

Why does pattern matching in Scala not work with variables?

Take the following function: def fMatch(s: String) = { s match { case "a" => println("It was a") case _ => println("It was something else") } } This pattern matches nicely: scala> fMatch("a") It was a scala> fMatch("b") It…
Henry Henrinson
  • 5,203
  • 7
  • 44
  • 76
109
votes
3 answers

Match multiple cases classes in scala

I'm doing matching against some case classes and would like to handle two of the cases in the same way. Something like this: abstract class Foo case class A extends Foo case class B(s:String) extends Foo case class C(s:String) extends Foo def…
timdisney
  • 5,287
  • 9
  • 35
  • 31
100
votes
1 answer

Elm Compiler running forever, computer just getting hot

I'm not sure what's causing this issue, but in a project, I'm building, the compiler is taking hours just to compile a module. The total size of my codebase is 352KB, but none of the modules are over 10KB large. I am using a Native port, but it's…
Athan Clark
  • 3,886
  • 2
  • 21
  • 39
98
votes
1 answer

Scala multiple type pattern matching

I'm wondering how can I use multiple type pattern matching. I have: abstract class MyAbstract case class MyFirst extends MyAbstract case class MySecond extends MyAbstract case class MyThird extends MyAbstract // shouldn't be matched and shouldn't…
psisoyev
  • 2,118
  • 1
  • 25
  • 35
97
votes
6 answers

Case insensitive search in Mongo

I am using a case insensitive search in Mongo, something similar to https://stackoverflow.com/q/5500823/1028488. ie. I am using a regex with options i. But I am having trouble restricting the regex to just that word, it performs more like a 'Like'…
Praneeta
  • 1,554
  • 3
  • 19
  • 20
95
votes
9 answers

How to print lines between two patterns, inclusive or exclusive (in sed, AWK or Perl)?

I have a file like the following and I would like to print the lines between two given patterns PAT1 and PAT2. 1 2 PAT1 3 - first block 4 PAT2 5 6 PAT1 7 - second block PAT2 8 9 PAT1 10 - third block I have read How to select lines between…
fedorqui
  • 275,237
  • 103
  • 548
  • 598
92
votes
2 answers

How to pattern match multiple values in Scala?

Let's say I want to handle multiple return values from a remote service using the same code. I don't know how to express this in Scala: code match { case "1" => // Whatever case "2" => // Same whatever case "3" => // Ah, something…
François Beausoleil
  • 16,265
  • 11
  • 67
  • 90
91
votes
9 answers

Merge Multiple spaces to single space; remove trailing/leading spaces

I want to merge multiple spaces into single space(space could be tab also) and remove trailing/leading spaces. For example... string <- "Hi buddy what's up Bro" to "Hi buddy what's up bro" I checked the solution given at Regex to…
CKM
  • 1,911
  • 2
  • 23
  • 30
90
votes
29 answers

byte[] array pattern search

Anyone know a good and effective way to search/match for a byte pattern in an byte[] array and then return the positions. For example byte[] pattern = new byte[] {12,3,5,76,8,0,6,125}; byte[] toBeSearched = new byte[]…
Anders R
  • 1,181
  • 2
  • 10
  • 10
88
votes
10 answers

How to override apply in a case class companion

So here's the situation. I want to define a case class like so: case class A(val s: String) and I want to define an object to ensure that when I create instances of the class, the value for 's' is always uppercase, like so: object A { def…
John S
  • 1,695
  • 2
  • 14
  • 25
88
votes
4 answers

How to compare enum without pattern matching

I want to apply filter on an iterator and I came up with this one and it works, but it's super verbose: .filter(|ref my_struct| match my_struct.my_enum { Unknown => false, _ => true }) I would rather write something like this: .filter(|ref…
Christoph
  • 26,519
  • 28
  • 95
  • 133
80
votes
11 answers

How to generate a number sequence in file using vi or Vim?

Is there a way to generate a number sequence in vi or Vim? For example, for an arbitrary range of lines i  through j (where i < j) in a file opened in Vim, is there a way to generate a number sequence from number 1 on line i all the way through…
Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
80
votes
2 answers

Pattern Matching `@` Symbol

Given this Person case class: scala> case class Person(name: String, age: Int) {} defined class Person ... and this instance scala> val b = Person("Kevin", 100) b: Person = Person(Kevin,100) Is there a reason to prefer this code (with @) scala> b…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
80
votes
3 answers

LOWER LIKE vs iLIKE

How does the performance of the following two query components compare? LOWER LIKE ... LOWER(description) LIKE '%abcde%' ... iLIKE ... description iLIKE '%abcde%' ...
user664833
  • 18,397
  • 19
  • 91
  • 140
80
votes
2 answers

In regex, match either the end of the string or a specific character

I have a string. The end is different, such as index.php?test=1&list=UL or index.php?list=UL&more=1. The one thing I'm looking for is &list=. How can I match it, whether it's in the middle of the string or it's at the end? So far I've got…
Gary
  • 3,891
  • 8
  • 38
  • 60