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

scheme pattern checking if it is a number

I am a scheme beginner and I am wondering how to explain this piece of scheme code? Looks so preculiar! (define (calc2 exp) (match exp [(? number? x) x])) I know match gives a pattern, but how to explain…
Alfred Zhong
  • 6,773
  • 11
  • 47
  • 59
4
votes
3 answers

Expression To Test That All Items In Sequence Are The Same

Is there a multiple instances pattern in F# somewhere? Consider that I'm working on a list. I have the following pattern matching match l with | [] | [_] -> l //if the list is empty or contains only one item, simply return it | //is…
Onorio Catenacci
  • 14,928
  • 14
  • 81
  • 132
4
votes
4 answers

removing single letter words using java pattern matching

I want to eliminate all single letter words from a string in Java using pattern matching. I've coded as follows: String str = "P@"; //remove single char words and extra white spaces inputStr = inputStr.replaceAll("\\b[\\w']{1}\\b",…
paras2682
  • 511
  • 2
  • 7
  • 14
4
votes
9 answers

Is there an inverse of grep: finding short lines in long patterns?

Where grep finds a short pattern from a pattern file in long lines of a look-up file, I need a tool that would allow me to extract short lines of a lookup file that can be found within a longer pattern. In other words, given the works of Shakespeare…
Etienne Low-Décarie
  • 13,063
  • 17
  • 65
  • 87
4
votes
5 answers

Find Non-exhaustive pattern

Can i somehow see what pattern haskell tries to use? It works on the smaller example, but crashes on the larger one, and I have a hard time finding what case it could be. euler18 :: [[Integer]] euler18 = pathPyr minipyr pathPyr xss = path [(head…
Viktor Mellgren
  • 4,318
  • 3
  • 42
  • 75
4
votes
2 answers

brute force string pattern matching average analysis

I have brute force string pattern searching algorithms as below: public static int brute(String text,String pattern) { int n = text.length(); // n is length of text. int m = pattern.length(); // m is length of pattern int j; for(int i=0; i…
venkysmarty
  • 11,099
  • 25
  • 101
  • 184
4
votes
2 answers

Nested pattern matching in Ocaml

I want to write a function in Ocaml that given a list of quadruples and a quadruple (x,y,z,f), returns a list of that contains the tuples (x',y',z',g) such that x = x' or y=y' or z = z' (these are integers). Here is my first attempt let rec…
Amin
  • 251
  • 2
  • 15
4
votes
6 answers

Oracle: Get length of partial string match

Imagine I have a table like Name ---- ABCDEFG ABChello world ABCDEfoo ABbar ABCDEF ABCDEFGHIJKLMNOP zzz qABCD ABCqqqGH ABCABC I want to do a query and figure out how many characters of each string match the desired string "ABCDEFGHIJ," always…
StilesCrisis
  • 15,972
  • 4
  • 39
  • 62
4
votes
1 answer

Pattern discovery in raw data

I am looking to construct an algorithm for discovering repeating patterns in raw data (non-ASCII). The shortest and largest pattern sizes to be configurable. The size of the data to search over would be in the tens of thousands of bytes. For…
Jon
  • 193
  • 2
  • 10
4
votes
3 answers

How to uncomment multiple lines on second pattern match using sed?

I am trying to use sed to uncomment a block of text in this config file. The code I came up with uncomments 7 lines starting from and including the pattern match on the first match but I need it to only work on the second match and skip the first…
Mason Mason
  • 67
  • 1
  • 1
  • 4
4
votes
2 answers

Which way of these two pattern matching is more preferred?

I'm just curious, these two functions would do the same thing. But which one should I use? let f a = match a with b -> a;; let f a = match a with b -> b;; Or it just depends on your preference? I feel the second one would…
4
votes
4 answers

bash: partial match up to a complete word for case

I wrote a bash script that takes a command as the first positional parameter and uses a case construct as a dispatch similar to the following: do_command() { # responds to invocation `$0 command ...` } do_copy() { # respond to invocation: `$0…
Iron Savior
  • 4,238
  • 3
  • 25
  • 30
4
votes
3 answers

Splitting string to parts with specific pattern and conditions

I have the below-like array of about 5k+ strings as output from certain application (for security reasons I may not provide the exact data, but the example format is pretty much similar to the actual…
Ksenia
  • 497
  • 5
  • 14
4
votes
2 answers

How to pattern match abstract parent classes in a inheritance tree

I am new to scala with a java background. Is there a way to pattern match super classes (or traits) in a class inheritance tree with leafs as case classes and nodes abstract classes or traits? As far as I know case class inheritance is not…
Kostas
  • 702
  • 3
  • 8
  • 17
4
votes
7 answers

Using the SQL LIKE operator with %%

I had a requirement to create a query in SQL Server where the search condition would include/exclude a table based on user input. Say I have two tables, TABLE_A and TABLE_B with columns KEYCOLUMN_A and COLUMN_A in TABLE_A and columns FKCOLUMN_B and…
devanalyst
  • 1,348
  • 4
  • 28
  • 55