pattern-matching

Regex in preg_replace to detect url format and extract elements

二次信任 提交于 2020-01-06 19:34:00
问题 I need to replace certain user-entered URLs with embedded flash objects...and I'm having trouble with a regex that I'm using to match the url...I think mainly because the URLs are SEO-friendly and therefore a bit more difficult to parse URL structure: http://www.site.com/item/item_title_that_can_include_1('_etc-32CHARACTERALPHANUMERICGUID I need to both detect a match of an URL in that format and capture the 32CHARACTERALPHANUMERICGUID which is always placed after the - in the url something

map expression in case clause in scala pattern matching

淺唱寂寞╮ 提交于 2020-01-06 12:51:11
问题 I have a configuration value that matches to one of the values in a map and depending on to which it matches i take an action. Here is some sample code of what i am trying to do val x = 1 // or 2 or 3 val config = Map("c1"-> 1, "c2"-> 2, "c3"-> 3) x match { case config("c1") => println("1") case config("c2") => println("2") case config("c3") => println("3") } Now this should print 1 because config("c1") evaluates to 1 but it gives error error: value config is not a case class, nor does it

Remove all lines which do not contain a period

假装没事ソ 提交于 2020-01-06 05:25:54
问题 I have a text like this in %userprofile%\i.txt : PDF wikkipedia.ord notavalidURL snapfish.com .tunnelbear.com mail.google.com/mail/u/0/#inbox I want to convert it to a string like this: wikkipedia.ord snapfish.com .tunnelbear.com mail.google.com/mail/u/0/#inbox! I want to do this using native cmd.exe syntax only (no powershell, cygwin, etc.). So I know that I can do this: @echo off setlocal EnableDelayedExpansion set row= for /f %%x in (%userprofile%\i.txt) do set "row=!row!%%x" echo %row% >

Sealed type parameter

南笙酒味 提交于 2020-01-05 12:12:13
问题 I was wondering if it would be possible to use Scala macros to generate something equivalent to the following: sealed type Foo type Bar <: Foo type Baz <: Foo Then the following expression would be recognized as not being exhaustive (foo: Foo) match { case bar: Bar => ??? } Looking at the comments in PatternMatching.scala, it looks like there could be a way to communicate those constraints to the typechecker. 回答1: type Bar <: Foo defines an abstract type member, it would be perfectly legal

Javascript / jQuery faster alternative to $.inArray when pattern matching strings

主宰稳场 提交于 2020-01-04 14:06:52
问题 I've got a large array of words in Javascript (~100,000), and I'd like to be able to quickly return a subset of them based on a text pattern. For example, I'd like to return all the words that begin with a pattern so typing hap should give me ["happy", "happiness", "happening", etc, etc] , as a result. If it's possible I'd like to do this without iterating over the entire array. Something like this is not working fast enough: // data contains an array of beginnings of words e.g. 'hap' $.each

LIKE query on elements of flat jsonb array

自闭症网瘾萝莉.ら 提交于 2020-01-04 09:29:24
问题 I have a Postgres table posts with a column of type jsonb which is basically a flat array of tags. What i need to do is to somehow run a LIKE query on that tags column elements so that i can find a posts which has a tags beginning with some partial string. Is such thing possible in Postgres? I'm constantly finding super complex examples and no one is ever describing such basic and simple scenario. My current code works fine for checking if there are posts having specific tags: select * from

Selecting a part of a string

…衆ロ難τιáo~ 提交于 2020-01-04 06:22:42
问题 I have a string stored in variable say $input = 999 success: id:10.123/AVC13231 | ark:/asf4523/2425fsaf I want to select only a part of a string "10.123/AVC13231" say i want to achieve this: $output = 10.123/XXXXXXXX ; and no other part $input should be selected even the id: part The value 10.123 is constant and the value AVC13231 changes dynamically. How can i achieve the above? 回答1: Here's a solution. $input = "999 success: id:10.123/AVC13231 | ark:/asf4523/2425fsaf"; $pos1 = strpos($input,

Pattern matching across type classes

落爺英雄遲暮 提交于 2020-01-04 05:26:10
问题 I'm a Haskell newbie, so chances are I've missed something obvious... I am trying to write a generic colour quantisation algorithm using ad hoc polymorphism. However, I'm having some trouble getting my data out with pattern matching (I've not actually got to the quantisation bit, yet). I can't describe this succinctly, so here's a simplified version of my code which exhibits the problem: {-# LANGUAGE FlexibleInstances #-} import Data.Word type ColourGrey = Word8 data ColourRGB = ColourRGB

how can i find a key in a map based on a pattern matching in scala

孤街醉人 提交于 2020-01-03 19:58:54
问题 I want to find a key in a map that is similar to a specific text. should I use for loop or is there a more elegant way? 回答1: The direct translation of your question is map.keys.find(_.matches(pattern)) which given a map, gets they keys and finds the first key that matches the regexp pattern. val map = Map("abc" -> 1, "aaa" -> 2, "cba" -> 3) map.keys.find(_.matches("abc.*")) // Some(abc) map.keys.find(_.matches("zyx")) // None A loop may be counter productive if you don't want to scan all keys

F# Pattern-matching by type

时间秒杀一切 提交于 2020-01-03 18:57:49
问题 How pattern-matching by type of argument works in F#? For example I'm trying to write simple program which would calculate square root if number provided or return it's argument otherwise. open System let my_sqrt x = match x with | :? float as f -> sqrt f | _ -> x printfn "Enter x" let x = Console.ReadLine() printfn "For x = %A result is %A" x (my_sqrt x) Console.ReadLine() I get this error: error FS0008: This runtime coercion or type test from type 'a to float involves an indeterminate type