pattern-matching

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 if..then..else". How would you explain it to them? EDIT! Thanks everyone for the great answers...

Pattern matching of lists in Python

I want to do some pattern matching on lists in Python. For example, in Haskell, I can do something like the following: fun (head : rest) = ... So when I pass in a list, head will be the first element, and rest will be the trailing elements. Likewise, in Python, I can automatically unpack tuples: (var1, var2) = func_that_returns_a_tu...

Pattern matching and placeholder values

Language: C# Hello, I'm writing an application that uses renaming rules to rename a list of files based on information given by the user. The files may be inconsistently named to begin with, or the filenames may be consistent. The user selects a list of files, and inputs information about the files (for MP3s, they would be Artist, Tit...

Ocaml: Match expression inside another one?

Hello, I'm currently working on a small project with Ocaml; a simple mathematical expression simplifier. I'm supposed to find certain patterns inside an expression, and simplify them so the number of parenthesis inside the expression decreases. So far I've been able to implement most rules except two, for which I've decided to create a r...

How do you read this JavaScript regular expression?

var pattern = /^0+$/; My guess is this: "Take a look at both the beginning and the end of the string, and if there's a pattern of one or more zeros at the beginning and the end, then return that pattern." I'm sure that's wrong, though, because when I run the expression with this string: var string = "0000009000000"; It comes up nu...

byte[] array pattern search

Hi, Anyone know a good and effective way to search/match for a byte pattern in an byte[] array and then return the positions. e.g. byte[] pattern = new byte[] {12,3,5,76,8,0,6,125}; byte[] toBeSearched = new byte[] {23,36,43,76,125,56,34,234,12,3,5,76,8,0,6,125,234,56,211,122,22,4,7,89,76,64,12,3,5,76,8,0,6,125} ...

How To Identify Email Belongs to Existing Thread or Conversation

We have an internal .NET case management application that automatically creates a new case from an email. I want to be able to identify other emails that are related to the original email so we can prevent duplicate cases from being created. I have observed that many, but not all, emails have a thread-index header that looks useful. ...

RegExp match one Word or Multiple Words in Quotes

I need a RegExp which matches a word or multiple words in quotes. [\w]* matches a word "[\w\W&&[^"]]*" matches multiple words in quotes. (btw, not sure why \w\W works, but not a simple . (which should match all characters) So how do i combine these two regexp? ...

regexp for finding everything between <a> and </a> tags

Hi, I'm trying to find a way to make a list of everything between <a> and </a> tags. So I have a list of links and I want to get the names of the links (not where the links go, but what they're called on the page). Would be really helpful to me. Currently I have this: $lines = preg_split("/\r?\n|\r/", $content); // content is the giv...

Algorithms recognizing physical address on a webpage

What are the best algorithms for recognizing structured data on an HTML page? For example Google will recognize the address of home/company in an email, and offers a map to this address. ...

Programatically compare two lines (stock pattern matching)

What I want to do is take a certain stock pattern (defined as a series of x and y coordinates) and compare it against historical stock prices. If I find anything in the historical prices similar to that pattern I defined, I would like to return it as a match. I'm not sure how to determine how similar two curved lines are. I did some r...

Regular Expression to extract label-value pairs in Java

I have a file containing several lines similar to: Name: Peter Address: St. Serrano número 12, España Country: Spain And I need to extract the address using a regular expression, taking into account that it can contain dots, special characters (ñ, ç), áéíóú... The current code works, but it looks quite ugly:. Pattern p = Pattern.com...

Any software for pattern-matching and -rewriting source code?

I have some old software (in a language that's not dead but is dead to me ;-)) that implements a basic pattern-matching and -rewriting system for source code. I am considering resurrecting this code, translating it into a modern language, and open-sourcing the project as a refactoring power-tool. Before I go much further, I want to know ...

Can you nest a "double cons" pattern match?

I want to strengthen a pattern to match only numbers which pass an additional validation function. let (|IsValid|_|) n = ... let (|Nil|One|Two|) (l : int list) = match l with | a :: b :: t -> Two(a + b) | a :: t -> One(a) | _ -> Nil The 'One' case is easy: | IsValid(a) :: t -> One(a) The 'Two' c...

Which is more efficient in Haskell; pattern matching or nested if/case statements?

I'm just curious about the efficiency of pattern matching in Haskell. What is a simple case of where pattern matching would be better than nested if/case statements and then the converse? Thanks for your help. ...

matching a line that doesn't contain specific text with regular expressions

I want to do the following with regular expressions but not sure how to do it. I want it to match "one two" when one two is the beginning of the line unless the string contains "three" anywhere after "one two". Note the " marks are just to represent string literals I don't need to match on them. ...

Which algorithm should I use for signal (sound) one class classification?

Update this question was previously titled as "Give me the name of a simple algorithm for signal(sound) pattern detection" My objective is to detect the presence of a given pattern in a noisy signal. I want to detect the presence of a species of insect recording the sounds with a microphone. I have previously recorded the sound of the ...

[F#] How do I use pattern matching in 'let' definitions?

I've just noticed F# allows me to use let bindings with literals and other patterns as follows: let fib 0 = 1 let exists item [] = false let car (hd :: tl) = hd let cdr (hd :: tl) = tl F# correctly interprets these functions as a kind of pattern matching, because gives me the following warnings: Warning 1 Incomplete pattern matc...

How to store sets, to find similar patterns fast?

(This is no homework and no work issue. It's just my personal interest/occupation and completly fictional. But I am interested in a good algorithm or data structure.) Let's assume, that I would run a dating site. And my special feature would be that the singles were matched by movie taste. (Why not?) In that case I would need a way to ...

Regular Expressions: Is there an AND operator?

Obviously, you can use | (pipe?), to represent OR, but can you match 'and' as well? Specifically, I'm wanting to match paragraphs of text that contain ALL of a certain phrase, but in no particular order. ...