pattern-matching

Spotting similarities and patterns within a string - Python

天大地大妈咪最大 提交于 2019-12-08 03:10:20
问题 this is the use case I'm trying to figure this out for. I have a list of spam subscriptions to a service and they are killing conversion rate and other usability studies. The emails inserted look like the following: rogerep_dyeepvu@hotmail.com rogeram_ingramameb@hotmail.com rogerew_jonesewct@hotmail.com roger[...]_surname[...]@hotmail.com What would be your suggestions on spotting these entries by using an automated script? It feels a little more complicated than it actually looks. Help would

pattern matching, tuples and multiplication in Python

两盒软妹~` 提交于 2019-12-08 01:28:40
问题 What is be best way to reduce this series of tuples ('x', 0.29, 'a') ('x', 0.04, 'a') ('x', 0.03, 'b') ('x', 0.02, 'b') ('x', 0.01, 'b') ('x', 0.20, 'c') ('x', 0.20, 'c') ('x', 0.10, 'c') into: ('x', 0.29 * 0.04 , 'a') ('x', 0.03 * 0.02 * 0.01, 'b') ('x', 0.20 * 0.20 * 0.10, 'c') EDIT: X is a constant, it is known in advance and can be safely ignored And the data can be treated as pre-sorted on the third element as it appears above. I am trying to do it at the moment using operator.mul, and a

Finding the start and stop indices in sequence in R

爷,独闯天下 提交于 2019-12-08 00:51:45
问题 Suppose I have the sequence: x = c( 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0) Is there an elegant way in R to return the start and stop indices of each sequence of 1s? The answer should be a 2 column array with nRows = number of sequences of 1s: startIndx = [ 1, 5, 7 ] stopIndex = [ 2, 5, 9 ] Thanks. BSL 回答1: Assuming your vector consists of 0 and 1 values: which(diff(c(0L, x)) == 1L) #[1] 1 5 7 which(diff(c(x, 0L)) == -1L) #[1] 2 5 9 Otherwise you'd need something like x <- x == 1L first. 回答2:

Java string matching with wildcards

Deadly 提交于 2019-12-08 00:41:00
问题 I have a pattern string with a wild card say X (E.g.: abc*). Also I have a set of strings which I have to match against the given pattern. E.g.: abf - false abc_fgh - true abcgafa - true fgabcafa - false I tried using regex for the same, it didn't work. Here is my code String pattern = "abc*"; String str = "abcdef"; Pattern regex = Pattern.compile(pattern); return regex.matcher(str).matches(); This returns false Is there any other way to make this work? Thanks 回答1: Just use bash style pattern

Debugging Pattern (regex) failure in in Java (Android)

ⅰ亾dé卋堺 提交于 2019-12-08 00:28:00
问题 I'm doing a pattern match in a piece of code that works fine in one circumstance but not another. The code is currently: DLVRYrx = Pattern.compile("(\\d+\\s\\p{Letter}+\\s\\d+)\\s(\\d+(?:\\.\\d+)?)\\s(\\d+)"); Log.d(TAG, "* Regex matched " + DLVRYrx.matcher("01 Jan 01 60.9876 1234").groupCount() + " groups"); // prints 3 as expected in logcat for (int i=19; i<(fields-6); i++) { final String DATAstr = values[i]; try { Matcher Dmatch = DLVRYrx.matcher(DATAstr); String data1 = Dmatch.group(0); }

jsonb LIKE query on nested objects in an array

孤街浪徒 提交于 2019-12-07 20:16:03
问题 My JSON data looks like this: [{ "id": 1, "payload": { "location": "NY", "details": [{ "name": "cafe", "cuisine": "mexican" }, { "name": "foody", "cuisine": "italian" } ] } }, { "id": 2, "payload": { "location": "NY", "details": [{ "name": "mbar", "cuisine": "mexican" }, { "name": "fdy", "cuisine": "italian" } ] } }] given a text "foo" I want to return all the tuples that have this substring. But I cannot figure out how to write the query for the same. I followed this related answer but

How do I replace a substring by the output of a shell command with sed, awk or such?

你说的曾经没有我的故事 提交于 2019-12-07 19:44:21
问题 I'd like to use sed or any command line tool to replace parts of lines by the output of shell commands. For example: Replace linux epochs by human-readable timestamps, by calling date Replace hexa dumps of a specific protocol packets by their decoded counterparts, by calling an in-house decoder sed seems best fitted because it allows to match patterns and reformat other things too, like moving bits of matches around, but is not mandatory. Here is a simplified example: echo "timestamp =

Scala pattern matching referencing

泄露秘密 提交于 2019-12-07 18:45:41
问题 When pattern matching case classes how do you actually refer to the class which it was matched to? Here's an example to show what I mean: sealed trait Value case class A(n: Int) extends Value v match { case A(x) => doSomething(A); } Where v is of type value and doSomething takes an parameter of type A , not Value . 回答1: Do this v match { case a @ A(x) => doSomething(a) } @ is called Pattern Binder (Refer § 8.1.3). From the reference: A pattern binder x@p consists of a pattern variable x and a

F# pattern matching of .Net constants

徘徊边缘 提交于 2019-12-07 15:08:41
问题 The code in the next example, open System.Drawing let testColor c = match c with | Color.Black -> 1 | Color.White -> 0 | _ -> failwith "unexpected color" doesn't compile. The error is Error 1 The field, constructor or member 'Black' is not defined . How do I pattern match against .Net constants or enums that start with capital letters? For what it's worth, the compiler is "Microsoft (R) F# 2.0 Interactive build 4.0.30319.1". 回答1: You can't pattern-match against arbitrary object values. Use an

How can I remove duplicates from a list in Scala with pattern matching?

一笑奈何 提交于 2019-12-07 13:13:40
问题 As homework i have to write a function that will remove duplicates from a list. It should be recursive and with pattern matching. I am not allowed to use list functions like head,tail,contains,etc... . For sorted lists i came up with this solution: def remove(u:List[Int]):List[Int] = { u match { case Nil => u case hd::hd2::tl => if(hd == hd2) remove(hd2::tl) else hd :: remove(hd2::tl) case hd::tl => hd :: remove(tl) } } How can i do it for unsorted lists? 回答1: I won't do your homework for you