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
78
votes
5 answers

Catching multiple exceptions at once in Scala

How to catch multiple exceptions at once in Scala? Is there a better way than in C#: Catch multiple exceptions at once?
TN.
  • 18,874
  • 30
  • 99
  • 157
77
votes
6 answers

How do I compare two arrays in scala?

val a: Array[Int] = Array(1,2,4,5) val b: Array[Int] = Array(1,2,4,5) a==b // false Is there a pattern-matching way to see if two arrays (or sequences) are equivalent?
Phil H
  • 19,928
  • 7
  • 68
  • 105
74
votes
9 answers

Explaining pattern matching vs switch

I have been trying to explain the difference between switch statements and pattern matching(F#) to a couple of people but I haven't really been able to explain it well..most of the time they just look at me and say "so why don't you just use…
Nathan W
  • 54,475
  • 27
  • 99
  • 146
72
votes
3 answers

Pattern matching on the beginning of a string in f#

I am trying to match the beginning of strings in f#. Not sure if I have to treat them as a list of characters or what. Any suggestions would be appreciated. Here is a psuedo code version of what I am trying to do let text = "The brown…
Jeff
  • 1,181
  • 1
  • 10
  • 17
71
votes
6 answers

What is the use of Pattern.quote method?

I'm trying to understand Pattern.quote using the following code: String pattern = Pattern.quote("1252343% 8 567 hdfg gf^$545"); System.out.println("Pattern is : "+pattern); produces the output: Pattern is : \Q1252343% 8 567 hdfg gf^$545\E What are…
Prateek
  • 12,014
  • 12
  • 60
  • 81
69
votes
1 answer

Glob matching, exclude all JS files

I'm a new user to gulp.js. I'd like to move all of my non-javascript files to a build directory. What I've got right now is this: //Test copy gulp.task('test-copy', function() { gulp.src(['myProject/src/**/*.!(js|map|src)']) …
AlexZ
  • 11,515
  • 3
  • 28
  • 42
68
votes
3 answers

What does x?.y?.z mean?

The draft spec for Pattern Matching in C# contains the following code example: Type? v = x?.y?.z; if (v.HasValue) { var value = v.GetValueOrDefault(); // code using value } I understand that Type? indicates that Type is nullable,…
tkocmathla
  • 901
  • 11
  • 24
66
votes
3 answers

Pattern match function against empty map

I'm playing around with pattern match and I found out, that it's not quite easy to pattern match parameters of a method against an empty map. I thought it would go something like this: defmodule PatternMatch do def modify(%{}) do %{} end …
leifg
  • 8,668
  • 13
  • 53
  • 79
65
votes
3 answers

SQL Server LIKE containing bracket characters

I am using SQL Server 2008. I have a table with the following column: sampleData (nvarchar(max)) The value for this column in some of these rows are lists formatted as follows: ["value1","value2","value3"] I'm trying to write a simple query that…
CJS
  • 1,455
  • 1
  • 13
  • 17
64
votes
6 answers

Check if string ends with certain pattern

If I have a string like: This.is.a.great.place.too.work. or: This/is/a/great/place/too/work/ than my program should give me that the sentence is valid and it has "work". If I Have…
The Learner
  • 3,867
  • 14
  • 40
  • 50
63
votes
8 answers

how to check if given c++ string or char* contains only digits?

Or from the other way around find first non digit character. Do the same functions apply for string and for char* ?
rsk82
  • 28,217
  • 50
  • 150
  • 240
61
votes
6 answers

Scala pattern matching on sequences other than Lists

I have the following code which recursively operates on each element within a List def doMatch(list: List[Int]): Unit = list match { case last :: Nil => println("Final element.") case head :: tail => println("Recursing...");…
Zecrates
  • 2,952
  • 6
  • 33
  • 50
61
votes
8 answers

JavaScript Regex Global Match Groups

Update: This question is a near duplicate of this I'm sure the answer to my question is out there, but I couldn't find the words to express it succinctly. I am trying to do the following with JavaScript regex: var input = "'Warehouse','Local…
Jondlm
  • 8,764
  • 2
  • 24
  • 30
60
votes
2 answers

Pattern matching with conjunctions (PatternA AND PatternB)

Scala has a language feature to support disjunctions in pattern matching ('Pattern Alternatives'): x match { case _: String | _: Int => case _ => } However, I often need to trigger an action if the scrutiny satisfies PatternA and PatternB…
retronym
  • 54,768
  • 12
  • 155
  • 168
58
votes
2 answers

Can we use match to check the type of a class

I'm new to scala, and I'm learning the match keyword now. I wanna know if we can use the keyword match to check the type of a class. My code is: object Main { def main(args: Array[String]) { val x = "AA" checkType(x) } …
Freewind
  • 193,756
  • 157
  • 432
  • 708