pattern-matching

Matching two overlapping patterns with Perl

一笑奈何 提交于 2019-12-04 06:06:27
I hope that my question has not already been posed by someone else, since I tried to look almost everywhere in the site but I couldn't manage to find an answer. My problem is: I'm making a PERL script which has to detect the position of every occurrence of one or another pattern in a string. For instance: $string = "betaalphabetabeta"; $pattern = "beta|alpha"; In this case, I would like my script to return 4 matches. I thought that this could be easily achieved by using the match operator in someway like this: $string =~ /beta|alpha/g; However, since my two patterns ("alpha", "beta") are

find and replace strings in files in a particular directory

允我心安 提交于 2019-12-04 05:45:38
问题 I have a pattern that I need to replace in my .hpp , .h , .cpp files in multiple directories. I have read Find and replace a particular term in multiple files question for guidance. I am also using this tutorial but I am not able achieve what I intend to do. So here is my pattern. throw some::lengthy::exception(); I want to replace it with this throw CreateException(some::lengthy::exception()); How can I achieve this? UPDATE: Moreover, what if the some::lengthy::exception() part is variant

Problem with Scala matching + scope

点点圈 提交于 2019-12-04 05:20:36
Given the following code: case class ChangeSet(field:String, from:Object, to:Object) private var changed:List[ChangeSet] = Nil def change(field:String, from:Object, to:Object) { changed.find{ case ChangeSet(field,_,_) => true } match { case Some(ChangeSet(field,to,_)) => // do stuff case Some(_) => // do stuff case _ => // do stuff } } The line giving me trouble is Some(ChangeSet(field,to,_)) . It compiles but what seems to be happening is that Scala is filling it in as a placeholder for a wildcard. I base that assumption on the fact that when I do the following Some(ChangeSet(field,to,to)) I

Surjectivity check when return type is sealed

妖精的绣舞 提交于 2019-12-04 05:16:08
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 sealed trait Foo case object Bar extends Foo case object Qux extends Foo Then function f: Foo => String on the algebraic data type Foo def f(x: Foo): String = x match { case Bar => "bar" } raises warning match may not be exhaustive. It would fail on the following input: Qux def f(x: Foo) = x match { Is it possible to raise a similar non-exhaustion warning when return type is an ADT such as in the

F# Records: Dangerous, only for limited use, or well used functionality?

血红的双手。 提交于 2019-12-04 05:13:44
So have gotten to record in my F# journey and at first they seem rather dangerous. At first this seemed clever: type Card = { Name : string; Phone : string; Ok : bool } let cardA = { Name = "Alf" ; Phone = "(206) 555-0157" ; Ok = false } The idea that the cardA is patten matched with Card. Not to mention the simplified pattern matching here: let withTrueOk = list |> Seq.filter (function | { Ok = true} -> true | _ -> false ) Problem is: type Card = { Name : string; Phone : string; Ok : bool } type CardTwo = { Name : string; Phone : string; Ok : bool } let cardA = { Name = "Alf" ; Phone = "(206)

Find a pattern to match 'a', ignoring that 'a' which lies within 'b' and 'c'

心不动则不痛 提交于 2019-12-04 05:06:45
问题 Need a compound expression for " from" such that " from" is not within parenthesis (ignoring those which are in parenthesis) here a=" from"; b="("; and c=")"; The closest (but invalid) pattern I could write is string pat = @"^((?!\(.* from.*\)).)* from((?!\(.* from.*\)).)*$"; my expression denies if any " from" is present in parenthesis but i want to strictly ignore such " from" Matches should be found in: 1: " from" 2:select field1 from t1 (select field1 from t1) ---- 1 time in both 3:

Cut string after first occurrence of a character

ε祈祈猫儿з 提交于 2019-12-04 05:02:55
I have strings like 'keepme:cutme' or 'string-without-separator' which should become respectively 'keepme' and 'string-without-separator'. Can this be done in PostgreSQL? I tried: select substring('first:last' from '.+:') But this leaves the : in and won't work if there is no : in the string. Erwin Brandstetter Use split_part() : SELECT split_part('first:last', ':', 1) AS first_part Returns the whole string if the delimiter is not there. And it's simple to get the 2nd or 3rd part etc. Substantially faster than functions using regular expression matching. And since we have a fixed delimiter we

How to find pattern groups in boolean array?

你离开我真会死。 提交于 2019-12-04 04:15:57
Given a 2D array of Boolean values I want to find all patterns that consist of at least 2 columns and at least 2 rows. The problem is somewhat close to finding cliques in a graph . In the example below green cells represent "true" bits, greys are "false". Pattern 1 contains cols 1,3,4 and 5 and rows 1 and 2. Pattern 2 contains only columns 2 and 4, and rows 2,3,4. Business idea behind this is finding similarity patterns among various groups of social network users. In real world number of rows can go up to 3E7, and the number of columns up to 300. Can't really figure out a solution other than

Switching on UIButton title: Expression pattern of type 'String' cannot match values of type 'String?!'

a 夏天 提交于 2019-12-04 03:39:06
I'm trying to use a switch in an @IBAction method, which is hooked to multiple buttons @IBAction func buttonClick(sender: AnyObject) { switch sender.currentTitle { case "Button1": print("Clicked Button1") case "Button2": print("Clicked Button2") default: break } When I try the above, I get the following error: Expression pattern of type 'String' cannot match values of type 'String?!' currentTitle is an optional so you need to unwrap it. Also, the type of sender should be UIButton since you are accessing the currentTitle property. @IBAction func buttonClick(sender: UIButton) { if let theTitle =

Parentheses in the pattern matching in a function declaration Haskell

人走茶凉 提交于 2019-12-04 03:28:32
问题 Do we have to use parentheses in pattern matching in function declarations ? In example below, I have a pattern x:xs where x takes first element from the list and xs contains the rest. I would like to ask whether parentheses are a necessary part of this pattern matching. head' :: [a] -> a head' [] = error "Can't call head on an empty list!" head' (x:_) = x I tried to use it without braces but it causes error during loading into ghci. 回答1: Parentheses are not part of pattern matching, in the