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

Pattern matching on abstract forms

Disclaimer: I kept this because some things may be useful to others, however, it does not solve what I had initially tried to do. Right now, I'm trying to solve the following: Given something like {a, B, {c, D}} I want to scan through Erlang forms…
justin
  • 3,357
  • 1
  • 23
  • 28
4
votes
1 answer

Matching an array to a row in Numpy

I have an array 'A' of shape(50,3) and another array 'B' of shape (1,3). Actually this B is a row in A. So I need to find its row location. I used np.where(A==B), but it gives the locations searched element wise. For example, below is the result i…
Abid Rahman K
  • 51,886
  • 31
  • 146
  • 157
4
votes
3 answers

OCaml Pattern match with non-constants

Is it possible to do pattern matching on variables instead of constant values: # let x = 2 in let y = 5 in match 2 with | x -> "foo" | y -> "bar" | _ -> "baz";; let y = 5 in Warning 26: unused variable y. let x = 2 in Warning 26:…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
4
votes
1 answer

Prolog, Finding the best match

I have several prolog facts: relation('Kitchen', [item(spoon), item(fork), item(knife) ]). relation('Lounge', [item(sofa), item(chair), item(table) ]). relation('Bedroom', [item(bed), item(desk), item(drawers)]). And a list that is…
LynchDev
  • 793
  • 1
  • 12
  • 27
3
votes
2 answers

Search pattern in string using regex in obj-c

I'm working on a string pattern match algorithm. I use NSRegularExpression for finding the matches. For ex: I've to find all words starting with '#' in a string.. Currently I use the following regex function: static NSRegularExpression…
manileo86
  • 153
  • 7
3
votes
1 answer

URL Match patterns in firefox addons

I am unable to figure out how can I use regular expression match patterns in Firefox addons (using Add-on Builder). I tried using match-pattern package. My main.js looks like this: var { MatchPattern } = require("match-pattern"); //Matching all urls…
Juzer Ali
  • 4,109
  • 3
  • 35
  • 62
3
votes
2 answers

How to get only the first matched value in an array

Edit: I meant to ask what is the best/fastest way to get the first match or the first xx matches. I have an array $arr = ('abc', 'ded', 'kjld', 'abr', 'cdfd'); I want to shuffle this array first, and then retrieve ONLY the first value that matches…
Jamex
  • 1,164
  • 5
  • 22
  • 34
3
votes
2 answers

pattern recognition between two very different images

So, my problem is that I have to find common points between two images of a microchip. Here's an example of two images: Between these two images, we can clearly see some common pattern like the wires on the bottom right of the first images that…
widgg
  • 1,358
  • 2
  • 16
  • 35
3
votes
2 answers

How to write scala matcher for class?

Assume I have follwing code def get[T](name:String)(implicit mf:ClassManifest[T]):T = mf.erasure match { case classOf[Boolean] => obj.getBoolean(name) case classOf[Int] => obj.getInt(name) } Now code dosn't work because classOf[Int] …
yura
  • 14,489
  • 21
  • 77
  • 126
3
votes
2 answers

Pattern matching on base class and all derived classes in Scala

I'm trying to achieve something like this: def a(b: Any) = { b match { case x: Seq[String] => println("x") } } // somewhere else a(List("b")) As a result I'd love to see "x" being printed, and I don't. Basically I want to match against a…
em70
  • 6,088
  • 6
  • 48
  • 80
3
votes
4 answers

PHP: Regex for exact match

Here is the regex I currently have (which kind of works): $regex = '/[\w ]{7,30}/'; My revision looks like what I want, but it does not work at all: $regex = '^[\w ]{7,30}$'; Here is how I am using the regex: public function isValid( $value ) { …
Nahydrin
  • 13,197
  • 12
  • 59
  • 101
3
votes
3 answers

Extending Scala pattern matching in subclass while maintaining complexity

I have an Scala question. Imagine you're building code to handle different operations, i.e. operation match { case A => doA() case B => doB() case _ => throw new Exception("Unknown op: " + operation) } Now, imagine that later on you wanna…
Galder Zamarreño
  • 5,027
  • 2
  • 26
  • 34
3
votes
1 answer

What algorithm is used in std::search?

There are many string matching algorithms can be used to find a pattern (string) in a big text, like Boyer-Moore, Aho-Corasick, etc. Which string matching algorithm is applied to implement std::search function in C++ ?
Aan
  • 12,247
  • 36
  • 89
  • 150
3
votes
2 answers

Using htaccess to force a trailing slash before the ? with a query string?

I have the following in my htaccess file: RewriteEngine On RewriteBase / # Check to see if the URL points to a valid file RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Trailing slash check RewriteCond %{REQUEST_URI}…
jeremiahs
  • 3,985
  • 8
  • 25
  • 30
3
votes
2 answers

Nant fileset basedir with pattern

I have an SVN externals folder which contains many folders called *Plugin then within each of these folders there is a modules folder and a binaries folder. The problem for me is, within my build task I want to copy **/* from modules to a location…
Grofit
  • 17,693
  • 24
  • 96
  • 176
1 2 3
99
100