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].

0
votes
1answer
37 views

data sharing dropWhile function in scala using pattern matching

Suppose you have the following functions available for reference sealed trait List[+A] case object Nil extends List[Nothing] case class Cons[+A](head: A, tail: List[A]) extends List[A] Now consider ...
1
vote
1answer
38 views

“Non-exhaustive patterns in function” error when appending value before function call

I'm not sure what I'm not handling. Suppose I have a function, that converts an integer to a string. Call it converter. Now, to convert position integer to string, I just call converter. To convert a ...
1
vote
1answer
27 views

Android string replacement using pattern

I am currently facing an issue in my android app when trying to search and replace a string. I receive a sentence which contain the keyword ${keyword} it's coming like Today we are ${keyword} and it ...
-1
votes
0answers
7 views

How to retrieve Names from e.g. a document header?

I am currently trying to find a way to search a pdf file by some variables, e.g. "Examiner name". Now i want to receive the actual values of those search words (in this case the actual name of the ...
1
vote
1answer
20 views

How to replace string patterns with some numbers using gsubfn

I have a dataset df1. I'd like to replace each occurence of "One + one," "Two ; one," etc. with some numbers as shown in the lookup table df2. Desired output: Any idea how to do this? This is a ...
0
votes
0answers
10 views

Image Processing - What is the advantage of normalizing SSD (Sum of Squared Differences)?

I am identifying the best algorithm for pattern matching using openCV for one specific application. The best performing algorithm was TM_SQDIFF_NORMED. What is the advantage of normalizing SSD ...
0
votes
1answer
13 views

Create synonyms and use regular expressions to find keyword

Background: I want to use regular expressions to search for a keyword. However, my keyword has multiple synonyms. For example, the keyword positive can have the following words that I consider as ...
0
votes
0answers
11 views

Python regular expression to match balanced parentheses [duplicate]

I need a regular expression to match the below line. I think some clever solution is required to match the text between balanced parenthesis. Example line: [link_name1](linkspec1(t1)) and [...
0
votes
0answers
20 views

grepl cannot handle unmatched parentheses [duplicate]

I am getting as input a string, and I need to check whether it contains a certain character. My expressions are sometimes unusual, like "[Diab", but they are in quotes. The way I try to do this is ...
4
votes
2answers
45 views

Scala pattern match and logical not

I have: x match { case a: SomeType => doSomething() case _ => doSomethingElse() } Is there a way to check that 'a' is NOT of a given type? like so: x match { case !(a: SomeType) =...
0
votes
1answer
47 views

Match clause is unsolvable. Behavior is not clear

I'm compiling queries programmatically at runtime and the match clause is the more general way I could think of to perform this task. Most of them are running fine but some, at some point, return the "...
0
votes
1answer
55 views

F# won't pattern match all elements of a list

I wrote a recursive function, doMoves, that should match each element of a list and call the designated function. However, I ran a list [Draw; Draw] in which Draw function was only called once. I'm ...
0
votes
1answer
45 views

How to compare values of a list and return true if all match?

I need to take a list of cards and check to see if their colors are the same. if they are return true otherwise return false. The function card_color is used to determine the color of a card. When I ...
0
votes
0answers
58 views

Incorrect 'non-exhaustive patterns' exception in Haskell? [duplicate]

I'm going through Learn you a Haskell and some of the examples don't seem to work. I'm assuming that tutorial is reputable enough, so I'm a bit surprised. A couple of examples, in GHCi 8.0.2: length' ...
0
votes
1answer
24 views

rabbitmq ldap authorization for groups with inconsistent user DNs

I'm trying to set up rabbitmq authorization against LDAP (Microsoft Active Directory) using the in_group or in_group_nested queries. However, since our OU structure is inconsistent across our users, ...
2
votes
2answers
44 views

Function returns the wrong value only when size of array input is 1

I have a function I wrote in Scala which iterates through an array of Options. I wish to return the first index for which the option is not None. My code works alright for all arrays of length greater ...
0
votes
2answers
18 views

How to match specific part of the URI string based on the following characters / words

I am trying to match particular part of the URI only when it is not followed by anything, or when it followed by '?'. .../survey?expand=all //should match .../survey //should match ...
5
votes
1answer
90 views

Pattern matching with type annotations

At https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/pattern-matching#tuple-pattern, there is this example of a pattern with a type annotation: Patterns can have type annotations. ...
1
vote
1answer
41 views

Trouble matching punctuation using regex in R

I am working on converting data into XML format. One of the XML constraints is that: Tag names cannot contain any of the characters !"#$%&'()*+,/;<=>?@[]^`{|}~, nor a space character, and ...
-1
votes
0answers
6 views

Find out how cancer factors influence cancer by the number of cancer cases and factors using SQL, Excel or other programming language

I need to find some way to relate cancer factors to the number of cancer cases. I have the number of cancer cases in the year 2018 with the factors that lead to the development of cancer, such as ...
1
vote
2answers
26 views

Match the whole cell of list1 to beginning of all cells of list2

The current question is the extension of this question. From previous question I have a code: list1<-list("q","w","e","r","t") list2<-list("a","a","aq","c","f","g") any(sapply(list1, grepl, ...
0
votes
0answers
21 views

Trying to pattern match partial file name in multiple folders using glob.glob

I am trying to find the newest version of files, each of which contains a unique reference code as part of their file name e.g. 12345. I have a large list of reference codes which are in pairs because ...
0
votes
1answer
62 views

Match exact string in an array in Perl

I want to match an exact string in Perl present between two escape characters in a file. I have tried the below codes, but they don't work when escape characters are present before and after the ...
2
votes
1answer
26 views

Matching peaks in similar spectra in python

I have a series of many thousands of (1D) spectra corresponding to different repetitions of an experiment. For each repetition, the same data has been recorded by two different instruments - so I have ...
1
vote
2answers
40 views

Why does Scala regexp work differently in pattern matching

I have a simple regular expression val emailRegex ="\\w+@\\w+\\.\\w+".r that matches simple emails (not for production, of course:). When I run the following code: println(email match { case ...
0
votes
2answers
32 views

Detect in list2 if there are any strings (whole or part of bigger string) that is contained in list1

I have two lists: list1<-list("q","w","e","r","t") list2<-list("a","a","aq","c","f","g") I need a code that will give TRUE because q is in the third cell of list2. I need to search for every ...
0
votes
1answer
15 views

Problems about placeholders of Netlify redirects

I am trying to set up some URL rewrites in Netlify. I want to set up a rewrite to redirect https://example.com/blog/2019/05/15/hello.html to https://example.com/blog/2019/05/hello.html I read ...
0
votes
1answer
16 views

Grep output of one and search in another grep

I want to get the output of some failing messages, and use the output to search in same file ( from beginning) I am able to get the first output, but how can I use that output to search in file ...
1
vote
3answers
30 views

How to make a pattern that check that all the first letter should be capital?

I need a pattern on angular that checks only if the first letter of each word will be capital. To Make something like this I am using this pattern pattern ="^([A-Z][a-z]*((\\s[A-Za-z])?[a-z]*)*)$" 1-...
0
votes
2answers
17 views

Grep for matching pattern but exclude particular string

I've file containing various log lines, and I want to grep for a pattern but exclude when message:com.mycompany.excluded , so basically following should be returned : "The log found this message blah,...
-4
votes
0answers
47 views

Match multiple string in a line and remove and replace, python [closed]

test.out file contains : AT 571 N1 XXX A 168 51.457 9.504 -13.406 1.00 74.97 AT 572 C1 XXX A 168 51.310 10.745 -12.657 1.00 71.22 AT 573 C2 XXX A 168 52.526 ...
6
votes
2answers
72 views

Surjectivity check when return type is sealed

Scala can warn when pattern match on a sealed type is not exhaustive, however can we check that a function returns all cases when the return type is sealed? For example, consider the following ADT ...
2
votes
1answer
29 views

Regular Expression OR in HTML

I am attempting to do the following: <input type="text" id="title" class="u-full-width" pattern="^(hi | hello)$" title "Some messege here"> Now what I am trying to make work is for the ...
1
vote
3answers
48 views

How to change a pattern like XX1/XXSomething/XX1/Something to XXSomething/XX1/Something in perl

I'm having a file in which some lines have some patterns like M1/XX2/XX1 XX2/XX1/XX2/WCLKB XX2/XX1/XX2/P001 M1/XX4/XX5 XX4/XX5/XX4/WCLKB XX4/XX5/XX4/P001 Here in some patterns XX2 is repeating. I ...
2
votes
3answers
50 views

list Pattern Matching to return a new list of every other element

I need to write a function that takes a List such as ("1","2","3") and return every other element in that list into a new list using pattern matching. What would be the correct case statement to get ...
0
votes
1answer
30 views

Substitute value of an attribute based on conditions in scala

I am working on an API module in scala which queries DB and returns the fetched data in JSON format as the response. For a particular attribute in the JSON(whose type is Option[String]) , I need to ...
1
vote
1answer
27 views

How can I find this pattern in a text document?

So im practicing some RegEx in python and essentially I want to look through a log of transaction numbers and see if any of them are returning an error such as Error in phone Activation. I was ...
0
votes
0answers
32 views

Is there a more efficient implementation of this recursive algorithm in grails?

I have a list of 'traits', L, and a database of lists that I'm matching to. I want to find lists that contain all traits in L. If I haven't yet collected 5, I remove one trait from L and search again ...
1
vote
3answers
38 views

Pattern not allowing words including numbers?

I wrote a program that lets me add categories, since it had some problems with special characters and numbers at the first position, I made a regex filter, which should only take care of the special ...
2
votes
2answers
34 views

Compilation failed: POSIX collating elements are not supported at offset

i would like transform a string to array with pattern. But my regex give me the warning. this is a string: $string = typ="bar" title="Example" enabled=true count=true style="float: left; width: 30%;"...
1
vote
4answers
54 views

Scala pattern matching for tuple with options still needs unwrapping for the some case

f(Int) is an function that returns an Option[Int]. def findIntPair(x: Int, y: Int): (Int, Int) = { (f(x), f(y)) match { case (None, None) || (None, _) || (_, None) => fail("Unable to ...
1
vote
2answers
34 views

How do I relax the non-exhaustive patterns check for a nested match on known variants?

How do I persuade the Rust compiler that the internal match expression is fine here, as the outer match has already restricted the possible types? enum Op { LoadX, LoadY, Add, } fn test(...
6
votes
1answer
106 views

Is it possible to use named parameter for Scala case-class matching? [duplicate]

Suppose there is a Scala case-class Point case class Point(x: Int, y: Int) One can use a wildcard for matching: val p = new Point(1,2) val inRightHalfPlane = p match { case Point(x, _) if x>0 =...
0
votes
3answers
111 views

Manipulating a list using pattern-matching

I want to write a function which takes a list as input value and manipulates it the following way: Step 1: Put every 3 elements of the list in a sublist. Should there remain less then 3 elements the ...
5
votes
2answers
156 views

Why do Haskell patterns have to be linear?

Example forbidden code (which I would like to be able to write): isWaiting :: Eq a => a -> PriorityQueue a -> Bool isWaiting x EmptyQueue = False isWaiting x (Push x y p) = True isWaiting ...
3
votes
1answer
76 views

Pattern Matching with float (Double type) in F#

Can anyone explain why F# doesn't seem to like floats (type Double) in pattern maching expressions? let intDiv x y = match x / y with | 5 -> printfn "Result was five - congrats"; 5 | z ...
1
vote
4answers
68 views

Simplify Massive Match Case - Scala

So in one of the places we have this huge variable match case statement. Containing almost 150 distinct case statements Looks horrible. I want to break it up into smaller functions, I can group the ...
-2
votes
1answer
61 views

Why do I get a borrow error when using Option::map, but not when manually matching?

I have this working function: fn clear_job(state: State<MyState>) -> Option<String> { let mut guard = state.job.lock().unwrap(); let maybe_job: &mut Option<Job> = &...
0
votes
1answer
52 views

F# Pattern matching on a generic type Map

This works : // sample objects let dctStrDbl = [("k1",1.0); ("k2",2.0)] |> Map.ofList let dctStrStr = [("k1","v1"); ("k2","v2")] |> Map.ofList let lstMisc = [1; 2; 3] let testStrDbl (...
0
votes
1answer
34 views

Type mismatch using pattern matching on constructors scala

I'm trying to define a HKT in Scala (a generic stream) and I'm not sure why I'm getting a type mismatch error while trying to implement the exists method: Here's my code so far sealed trait ...

http://mssss.yulina-kosm.ru