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

Perl Regex Regular Expression match string except, not match string

Anyone be able to help me with this regex please? I need an expression that will match the line that does not contain the "Created" string at the end. This script is being used to read the headings on some source code. $string = "* JAN-01-2001 …
John Lee
  • 1,357
  • 1
  • 13
  • 26
4
votes
1 answer

Extractor: inferred type arguments X do not conform to method unapply's type parameter bounds

In the following example, Scala cannot use the extractor, and it is driving me mad: trait Sys[S <: Sys[S]] object Element { object Foo { def unapply[S <: Sys[S]](foo: Foo[S]): Option[Any] = ??? } trait Foo[S <: Sys[S]] extends…
0__
  • 66,707
  • 21
  • 171
  • 266
4
votes
1 answer

Pattern Matching in Type Synonyms / Type Synonyms for Inner Type Parameters

I have something similar to this class in my code. It does not make sense for my situation to add a' as another parameter to class Foo. class Foo a where type FCtx a a' :: Constraint type FCtx a a' = () f :: (FCtx a a') => a -> a' data…
crockeea
  • 21,651
  • 10
  • 48
  • 101
4
votes
3 answers

PostgreSQL various clean up of string \ varchar

I have to clean up some varchar in the following manner: Remove special characters such as: !, @, #, $, %, ^, &, *, (, ), }, {, [, ], ",", ., ?, /, ', from a closed list. I've managed to do so with a mass use of replace\regexp_replace but I'm…
gilibi
  • 343
  • 2
  • 9
  • 18
4
votes
3 answers

Extract numbers from string with rich string magic

I want to extract a list of ID of a string pattern in the following: {(2),(4),(5),(100)} Note: no leading or trailing spaces. The List can have up to 1000 IDs. I want to use rich string pattern matching to do this. But I tried for 20 minutes with…
jerry
  • 355
  • 1
  • 6
  • 13
4
votes
3 answers

Scala: toSeq vs Seq(something:_*)

I wrote a function named extract, defined as follows: def extract(params: String): Seq[String] = { val result = params.split(",") map (param => param.trim()) result toSeq } Then I perform pattern matching over extract result, like…
fedragon
  • 884
  • 9
  • 10
4
votes
1 answer

Scala pattern matching option None with or without binding

I would like to do the following pattern matching : minReachableInt match { case None | Some(n) if n <= 0 => println("All positive numbers can be reached") case _ => println("Not all positive numbers can be reached") } Of course, it…
Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101
4
votes
3 answers

How to pattern match this efficiently?

I would like to match an integer array against the following pseudo patterns: where a means these numbers are the equal or 0 (i.e. assume any number "equals" 0) [| a; a; a; a; a; |] // matches [| 1; 1; 0; 0; 1 |] [| a; a; a; not a; a; |] …
colinfang
  • 20,909
  • 19
  • 90
  • 173
4
votes
3 answers

How to escape square bracket when using LIKE?

Possible Duplicate: SQL Server LIKE containing bracket characters I am having a problem with pattern matching.I have created two objects say,with codes 1)[blah1] 2)[blah2] respectively in the search tab,suppose if i give "[blah" as the…
4
votes
1 answer

Normalizing by max value or by total value?

I'm doing some work that involves document comparison. To do this, I'm analizing each document, and basically counting the number of times some key words appear on each of these documents. For instance: Document 1: Document…
juliomalegria
  • 24,229
  • 14
  • 73
  • 89
4
votes
2 answers

Discovering Consecutive Repetitive Patterns in a String

I am trying to search for the maximal number of substring repetitions inside a string, here are some few examples: "AQMQMB" => QM (2x) "AQMPQMB" => "AACABABCABCABCP" => A (2x), AB (2x), ABC (3x) As you can see I am searching for…
Y.H.
  • 2,687
  • 1
  • 29
  • 38
4
votes
4 answers

scala: Cannot check match for unreachability

I'm migrating an app from play 2.0.4 to play 2.1 But the following code raises this warning: def toConditionOperator(value: String): ConditionOperator.Value = { if (value==null) { ConditionOperator.Unknown } else { value.toLowerCase…
opensas
  • 60,462
  • 79
  • 252
  • 386
4
votes
1 answer

Algorithms for Unification of list-based trees

I need a unification algorithm to handle the following situation. Each node in my expression tree has a list (associative) or a set (associative and commutative) of children. I would like to get all possible matches to a specific pattern. Here is…
4
votes
4 answers

Haskell pattern matching char in a string

I have a question on pattern matching: Is it possible to somehow match a (string ++ [char] ++ anotherstring)? I have tried something like: f (s++";"++r) = s++r (the rhs is trivial, but its just for testing ;)) But this results in a parse error.
Stackd
  • 683
  • 1
  • 6
  • 12
4
votes
1 answer

Efficient matching of a variable to a value stored in an array

I've searched for other threads with a similar problem, but I couldn't find any that apply to me. If I have a variable which has some value, and an array that has a list of values... is it possible for me to efficiently (time efficient, space isn't…
Kitchi
  • 1,874
  • 4
  • 28
  • 46