pattern-matching

Pattern to extract text between parenthesis

无人久伴 提交于 2019-12-17 18:28:06
问题 How to extract string from "(" and ")" using pattern matching or anything. For example if the text is ` "Hello (Java)" Then how to get only "Java" . Thanks. 回答1: Try this: String x= "Hello (Java)"; Matcher m = Pattern.compile("\\((.*?)\\)").matcher(x); while(m.find()) { System.out.println(m.group(1)); } or String str = "Hello (Java)"; String answer = str.substring(str.indexOf("(")+1,str.indexOf(")")); 回答2: List<String> matchList = new ArrayList<String>(); Pattern regex = Pattern.compile("\\((

Pattern to extract text between parenthesis

怎甘沉沦 提交于 2019-12-17 18:27:03
问题 How to extract string from "(" and ")" using pattern matching or anything. For example if the text is ` "Hello (Java)" Then how to get only "Java" . Thanks. 回答1: Try this: String x= "Hello (Java)"; Matcher m = Pattern.compile("\\((.*?)\\)").matcher(x); while(m.find()) { System.out.println(m.group(1)); } or String str = "Hello (Java)"; String answer = str.substring(str.indexOf("(")+1,str.indexOf(")")); 回答2: List<String> matchList = new ArrayList<String>(); Pattern regex = Pattern.compile("\\((

Scala: Ignore case class field for equals/hascode?

谁都会走 提交于 2019-12-17 17:39:26
问题 Is it possible to ignore a field of a case class in the equals/haschode method of the case class? My use case is that I have a field that is essentially metadata for rest of the data in the class. 回答1: Only parameters in the first parameter section are considered for equality and hashing. scala> case class Foo(a: Int)(b: Int) defined class Foo scala> Foo(0)(0) == Foo(0)(1) res0: Boolean = true scala> Seq(0, 1).map(Foo(0)(_).hashCode) res1: Seq[Int] = List(-1669410282, -1669410282) UPDATE To

How do I compare two arrays in scala?

筅森魡賤 提交于 2019-12-17 17:35:14
问题 val a: Array[Int] = Array(1,2,4,5) val b: Array[Int] = Array(1,2,4,5) a==b // false Is there a pattern-matching way to see if two arrays (or sequences) are equivalent? 回答1: You need to change your last line to a.deep == b.deep to do a deep comparison of the arrays. 回答2: From Programming Scala: Array(1,2,4,5).sameElements(Array(1,2,4,5)) 回答3: a.corresponds(b){_ == _} Scaladoc: true if both sequences have the same length and p(x, y) is true for all corresponding elements x of this wrapped array

LOWER LIKE vs iLIKE

无人久伴 提交于 2019-12-17 17:32:25
问题 How does the performance of the following two query components compare? LOWER LIKE ... LOWER(description) LIKE '%abcde%' ... iLIKE ... description iLIKE '%abcde%' ... 回答1: The answer depends on many factors like Postgres version, encoding and locale - LC_COLLATE in particular. The bare expression lower(description) LIKE '%abc%' is typically a bit faster than description ILIKE '%abc%' , and either is a bit faster than the equivalent regular expression: description ~* 'abc' . This matters for

How can I pattern match on a range in Scala?

谁都会走 提交于 2019-12-17 16:00:31
问题 In Ruby I can write this: case n when 0...5 then "less than five" when 5...10 then "less than ten" else "a lot" end How do I do this in Scala? Edit: preferably I'd like to do it more elegantly than using if . 回答1: Inside pattern match it can be expressed with guards: n match { case it if 0 until 5 contains it => "less than five" case it if 5 until 10 contains it => "less than ten" case _ => "a lot" } 回答2: class Contains(r: Range) { def unapply(i: Int): Boolean = r contains i } val C1 = new

Case insensitive search in Mongo

百般思念 提交于 2019-12-17 15:39:44
问题 I am using a case insensitive search in Mongo, something similar to https://stackoverflow.com/q/5500823/1028488. ie I am using a regex with options i. But I am am having trouble restricting the regex to just that word, its performs more like a 'Like' in SQL eg: if I use query like {"SearchWord" : { '$regex' : 'win', $options: '-i' }} , it shows me results for win, window & winter. How do i restrict it to jsut show win? I tried /^win$/ but its sayin invalid Json.... Please suggest a way.

Scala pattern matching on sequences other than Lists

时光总嘲笑我的痴心妄想 提交于 2019-12-17 15:38:02
问题 I have the following code which recursively operates on each element within a List def doMatch(list: List[Int]): Unit = list match { case last :: Nil => println("Final element.") case head :: tail => println("Recursing..."); doMatch(tail) } Now, ignoring that this functionality is available through filter() and foreach() , this works just fine. However, if I try to change it to accept any Seq[Int] , I run into problems: Seq doesn't have ::, but it does have +:, which as I understand is

KMP prefix table

孤人 提交于 2019-12-17 15:23:49
问题 I am reading about KMP for string matching. It needs a preprocessing of the pattern by building a prefix table. For example for the string ababaca the prefix table is: P = [0, 0, 1, 2, 3, 0, 1] But I am not clear on what does the numbers show. I read that it helps to find matches of the pattern when it shifts but I can not connect this info with the numbers in the table. 回答1: Every number belongs to corresponding prefix ("a", "ab", "aba", ...) and for each prefix it represents length of

How is this case class match pattern working?

陌路散爱 提交于 2019-12-17 11:45:49
问题 I've just seen this case class in the Scala actors package: case class ! [a](ch: Channel[a], msg: a) And in the JavaDoc it describes usage in the following form: receive { case Chan1 ! msg1 => ... case Chan2 ! msg2 => ... } Why is this not: receive { case !(Chan1, msg1) => ... case !(Chan2, msg2) => ... } Is the bang operator ! a special case in a similar way to methods ending in a colon : 回答1: When doing pattern matching, the Scala compiler will interpret o1 c1 o2 the same as c1(o1, o2) .