pattern-matching

How to string.find the square bracket character in lua?

允我心安 提交于 2019-12-04 19:56:49
问题 So I'm trying to find square brackets inside a string: s = "testing [something] something else" x,y = string.find(s,"[") which gives me an error: malformed pattern (missing ']'). I also tried: x,y = string.find(s,"\[") giving me the same error. And this: x,y = string.find(s,"\\[") in this case x and y are nil. Any thoughts on how to do this properly? Thanks in advance. 回答1: John's answer will work -- turning off pattern matching. What you're trying to do -- escape the [ -- is done with the %

How to count occurrence of each character in java string using Pattern Class(Regex)

末鹿安然 提交于 2019-12-04 19:21:22
I am trying to find a number of Occurrence of each character on the given string. Expected output: t=2 e=1 s=1 i=1 n=1 g=1 Current output: T=0 e=0 s=0 t=0 i=0 n=0 g=0 Code: String str = "Testing"; int count = 0; Pattern p = Pattern.compile("[a-zA-Z]", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(str); while (m.find()) { if (m.group().equals(str)) { count++; } System.out.println(m.group() + "=" + count); } There are many ways of doing this but I am looking for Regex only, so how can we achieve that by using Regex. Any Help would be Appreciated. Thanks in advance. No need for a regex to

multiline regexp matching in bash

谁说我不能喝 提交于 2019-12-04 18:47:42
问题 I would like to do some multiline matching with bash's =~ #!/bin/bash str='foo = 1 2 3 bar = what about 42? boo = more words ' re='bar = (.*)' if [[ "$str" =~ $re ]]; then echo "${BASH_REMATCH[1]}" else echo no match fi Almost there, but if I use ^ or $ , it will not match, and if I don't use them, . eats newlines too. EDIT: sorry, values after = could be multi-word values. 回答1: I could be wrong, but after a quick read from here, especially Note 2 at the end of the page, bash can sometimes

Mathematica's pattern matching poorly optimized?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 17:10:00
问题 I recently inquired about why PatternTest was causing a multitude of needless evaluations: PatternTest not optimized? Leonid replied that it is necessary for what seems to me as a rather questionable method. I can accept that, though I would prefer a more efficient alternative. I now realize, which I believe Leonid has been saying for some time, that this problem runs much deeper in Mathematica , and I am troubled. I cannot understand why this is not or cannot be better optimized. Consider

Warning about an unchecked type argument in this Scala pattern match?

China☆狼群 提交于 2019-12-04 17:09:52
问题 This file: object Test extends App { val obj = List(1,2,3) : Object val res = obj match { case Seq(1,2,3) => "first" case _ => "other" } println(res) } Gives this warning: Test.scala:6: warning: non variable type-argument A in type pattern Seq[A] is unchecked since it is eliminated by erasure case Seq(1,2,3) => "first" Scala version 2.9.0.1. I don't see how an erased type parameter is needed to perform the match. That first case clause is meant to ask if obj is a Seq with 3 elements equal to

How to extract text between two words in unix?

旧巷老猫 提交于 2019-12-04 16:40:14
问题 I am using basic sed expression :- sed -n "am/,/sed/p" to get the text between "am" and "sed" which will output "am \n using \n basic \n sed". But my real problem is if the string would be :- I am using basic grep expression. I applied the above sed in this sentence then it gave "am \n using \n basic \n grep \n expression" which it should not give it. How to discard the output if there would be no matching? Any suggestions? 回答1: The command in the question ( sed -n "/am/,/sed/p" , note the

Is it possible to match with decomposed sequences in F#?

六眼飞鱼酱① 提交于 2019-12-04 16:21:57
问题 I seem to remember an older version of F# allowing structural decomposition when matching sequences just like lists. Is there a way to use the list syntax while keeping the sequence lazy? I'm hoping to avoid a lot of calls to Seq.head and Seq.skip 1. I'm hoping for something like: let decomposable (xs:seq<'a>) = match xs with | h :: t -> true | _ -> false seq{ 1..100 } |> decomposable But this only handles lists and gives a type error when using sequences. When using List.of_seq, it seems to

What causes “irrefutable pattern failed for pattern” and what does it mean?

偶尔善良 提交于 2019-12-04 15:31:26
问题 What does irrefutable pattern failed for pattern mean? What cases will cause this runtime error? 回答1: Well, I assume it means what it says - that a pattern doesn't match but there is no alternative. This example: But for the program: g x = let Just y = f x in h y GHC reports: Main: M1.hs:9:11-22: Irrefutable pattern failed for pattern Data.Maybe.Just y Indicating the source of the failure. Comes from http://www.haskell.org/haskellwiki/Debugging The point of the example is that if f x returns

Is is possible to pattern match on the underlying shape of a discriminated union?

痞子三分冷 提交于 2019-12-04 15:25:21
Does F# support pattern matching of a discriminated union member instance by criteria other than the Identifier pattern ? For example, imagine that I want to match on the underlying shape of the data and I want to consider anything with an int * int shape, regardless of how the DU classifies the value. Is Here's how I'd do it now: type ExampleDU = | BinaryCase1 of x:int * y:int | BinaryCase2 of x:int * y:int | UnaryCase1 of x:int let underlyingValue = (1,2) let asCase1 = BinaryCase1 underlyingValue let asCase2 = BinaryCase2 underlyingValue let shapeName = match asCase1 with | BinaryCase1 (x,y)

Simplifying nested Maybe pattern matching

懵懂的女人 提交于 2019-12-04 13:42:40
I have the following construct in my code: f :: Maybe A -> X f a = case a of Nothing -> x (Just b) -> case b of Nothing -> y (Just c) -> case c of Nothing -> z (Just d) -> d I'm not seeing an obvious way to simplify this instead of using nested maybe functions, which wouldn't make the whole thing look much better. Are there any clever - but still understandable - tricks that would help make this construct more "elegant"? UPDATED 2 Monad Either is for you import Data.Maybe (maybe) maybeE :: e -> Maybe a -> Either e a maybeE e = maybe (Left e) Right f :: Maybe (Maybe (Maybe d)) -> Either e d f a