标签: pattern-matching

如何使用扫描仪和模式类匹配文本中的模式?

我想查找我的文本文件中是否存在特定模式.

即时通讯使用以下类:

java.util.regex.Pattern and java.util.Scanner;
Run Code Online (Sandbox Code Playgroud)

我的示例文本行是

String Line="DBREF  1A1F A  102   190  UNP    P08046   EGR1_MOUSE     308    396";
Run Code Online (Sandbox Code Playgroud)

而且,我想匹配以下类型的模式:

A    102   190
Run Code Online (Sandbox Code Playgroud)

其中,在A的位置az或AZ但是单个包机.

在102的位置任何整数和任何长度.

在190的位置任何整数和任何长度.

而且,我的模式匹配代码是:

     Scanner sr=new Scanner(Line);
     Pattern p = Pattern.compile("\\s+([a-zA-Z]){1}\\s+\\d{1,}\\s+\\d{1,}\\s+");
     while(sr.hasNext(p))
     {
         System.out.println("Pattern exists");
         System.out.println("Matched String : "+sr.next(p));
     }
Run Code Online (Sandbox Code Playgroud)

但是,即使它存在,模式也不匹配..

我认为问题在于我的模式字符串:

\\s+([a-zA-Z]){1}\\s+\\d{1,}\\s+\\d{1,}\\s+"
Run Code Online (Sandbox Code Playgroud)

任何人,Plz帮助我应该使用什么模式字符串????

java regex pattern-matching java.util.scanner

7
推荐指数
1
解决办法
2万
查看次数

如何使用模式匹配检测Ocaml中的交换模式?

我需要在我的一个函数中检测到一个可交换模式.我认为编写以下内容将完成工作:

let my_fun a b = match a,b with
  (*...*)
  | a,b
  | b,a when is_valid b -> process b  (***)
  (*...*)
Run Code Online (Sandbox Code Playgroud)

这不起作用,Ocaml抱怨这个子模式是 标记为的行的未使用警告(***).

1)有人可以向我解释这个警告试图说什么以及为什么这不起作用?

2)如果不if then else考虑我现在想要哪个参数的事实,我怎样才能真正优雅地写出这个is_valid?

2)是否可以仅使用模式匹配来获得预期的功能而不重复,when is_valid b -> process b因为它发生在下面?

let my_fun a b = match a,b with
  (*...*)
  | a,b when is_valid b -> process b
  | b,a when is_valid b -> process b 
  (*...*)
Run Code Online (Sandbox Code Playgroud)

编辑:

在我的具体例子中a,b是成对的.该功能有点复杂,但以下将说明这种情况:

let f a …
Run Code Online (Sandbox Code Playgroud)

warnings ocaml pattern-matching

7
推荐指数
1
解决办法
346
查看次数

Haskell - 模式匹配重叠

test :: String -> String -> Int

test' x y n = n
test' "" (y:ys) n = error "error"
test' (x:xs) "" n = error "error"
test' (x:xs) (y:ys) n =
        if      x == y
        then    test'  xs ys n
        else    test'  xs ys (n+1)
test a b = test' a b 0
Run Code Online (Sandbox Code Playgroud)

当我编译它时,我得到这个输出:

Warning: Pattern match(es) are overlapped
Run Code Online (Sandbox Code Playgroud)

答案总是"0",这不是我的意图.代码有什么问题以及如何修复它?

haskell pattern-matching

7
推荐指数
2
解决办法
9111
查看次数

模式匹配后失去多态性

以下代码旨在生成Double或Integer.s被假定为negate或id; n整个部分; 和f小数部分或Nothing整数.

computeValue :: Num a => (a->a) -> Integer -> (Maybe Double) -> Either Double Integer
computeValue s n Nothing = Right $ s n
computeValue s n (Just a) = Left $ s (fromIntegral n + a)
Run Code Online (Sandbox Code Playgroud)

当我编译这个时,我得到:

test1.hs:2:28:
    Couldn't match type `Integer' with `Double'
    Expected type: Either Double Integer
      Actual type: Either Double a
    In the expression: Right $ s n
    In an equation for …
Run Code Online (Sandbox Code Playgroud)

polymorphism haskell pattern-matching

7
推荐指数
1
解决办法
359
查看次数

scala是否有"test-if-match"运算符?

可能重复:
Scala:返回布尔值的简短模式匹配形式

在我的scala代码中,我发现自己经常编写如下内容:

x match{
   case Type(params) => doStuffWith(params)
   case _ => /* do nothing*/
}
Run Code Online (Sandbox Code Playgroud)

是否已有一些预定义的运算符来执行此操作?我想如果我能写下这样的东西会更清楚:

if( x match Type(params)) {
    doStuffWith(params)
}
Run Code Online (Sandbox Code Playgroud)

基本上避免了奇怪的情况.我还有其他情况可以验证某些东西是否与内联方式相匹配,这样可以省去额外的一对括号.

我知道在编写更多迭代代码时这种事情可能更有用,但Scala似乎有很多隐藏的功能,我想知道是否有人有一个简单的解决方案.

scala pattern-matching

7
推荐指数
1
解决办法
723
查看次数

在限制带有类型族的GADT时,摆脱"非详尽的模式匹配"警告

鉴于以下计划:

{-# LANGUAGE DataKinds, GADTs #-}
{-# LANGUAGE TypeFamilies #-}

data Foo = A | B
type family IsA (foo :: Foo) :: Bool

type instance IsA A = True
type instance IsA B = False

data Bar (foo :: Foo) where
    BarA :: (IsA foo ~ True) => Int -> Bar foo
    BarB :: (IsA foo ~ False) => String -> Bar foo

f :: Bar A -> Int
f bar = case bar of
    BarA x -> x …
Run Code Online (Sandbox Code Playgroud)

warnings haskell pattern-matching ghc type-families

7
推荐指数
1
解决办法
735
查看次数

F#使用泛型类型作为模式鉴别器

如果还有另一种方法可以实现我在下面尝试做的事情,请告诉我.假设我有以下示例代码

type FooBar = 
  | Foo
  | Bar

let foobars = [Bar;Foo;Bar]

let isFoo item  = 
  match item with
  | Foo _ -> true
  | _ -> false

foobars |> Seq.filter isFoo
Run Code Online (Sandbox Code Playgroud)

我想编写isFoo的通用/高阶版本,它允许我根据所有其他类型的区分联合(在这种情况下为Bar)过滤我的列表.

像下面的东西,'可以是Foo或Bar

let is<'a> item  = 
  match item with
  | a _ -> true
  | _ -> false
Run Code Online (Sandbox Code Playgroud)

但是,此尝试会产生以下错误:

错误FS0039:未定义模式鉴别器"a"

f# pattern-matching discriminated-union

7
推荐指数
1
解决办法
2539
查看次数

MySQL MATCH AGAINST无效

我实际上在一个有两张桌子的网站上工作,几乎完全相同,其中MATCH AGAINST在一个上面但在另一个上面不起作用.要找出为什么我试图将其简化为一个简单的"让我们做一个简单的表格工作" - 但事实并非如此.

我正在使用phpMyAdmin进行测试,这是MySQL 5.1.41.

我构建的测试使用以下来定义表...

CREATE TABLE IF NOT EXISTS `test` (
  `id` int(11) NOT NULL,
  `title` text NOT NULL,
  `body` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

ALTER TABLE `test` ADD FULLTEXT (`title`, `body`);
Run Code Online (Sandbox Code Playgroud)

当我做一个SHOW INDEX FROM测试时,我看到有一个包含标题和正文的FULLTEXT键.

我插入几行

INSERT INTO `test` (`id`, `title`, `body`) VALUES 
('1', 'Lorem Ipsum', 'Lorem ipsum dolor sit amet, consectetur ... lacus porta euismod.'), 
('2', 'Lorem Ipsum (cont)', 'Nunc leo massa, vulputate ... euismod fringilla.');
Run Code Online (Sandbox Code Playgroud)

(为简洁起见,删除了一些正文内容)

然后当我跑

SELECT * FROM …
Run Code Online (Sandbox Code Playgroud)

mysql pattern-matching match

7
推荐指数
2
解决办法
1万
查看次数

Haskell是否允许let表达多个模式匹配?

假设我有一个函数可以进行一些计算,有几种模式; 以模式匹配的形式实现.

大多数这些模式(以及其他不同的东西)对参数进行处理,我在let表达式中使用中间变量.但我发现let在许多模式上使用相同的方法确实是多余的,我想知道是否有一种方法可以let为多种模式定义?

这是我复制的一个例子let:

data MyType a = Something a | Another Int [a]

myFunc (Something x) = -- return something, this isn't the point here
myFunc (Another 0 xs) =
    let intermediary = some $ treatment xs
    in doSthg intermediary 1 
myFunc (Another 1 (x:xs)) =
    let intermediary = some $ treatment xs
    in doSthg1 intermediary 1 x
myFunc (Another 2 (x:x':xs)) =
    let intermediary = some $ treatment xs
    in …
Run Code Online (Sandbox Code Playgroud)

haskell functional-programming pattern-matching let

7
推荐指数
1
解决办法
6304
查看次数

Scala中非案例类的模式匹配

让我们假设我有一个简单的第三方(即我不能修改它)类定义如下:

class Price(var value: Int)
Run Code Online (Sandbox Code Playgroud)

是否可以将此类的实例与某些模式匹配?

例如,我想实现功能:

def printPrice(price: Price) = {
    // implementation here
}
Run Code Online (Sandbox Code Playgroud)

...打印price is {some value}每个price有value <= 9000和price is over 9000所有其他情况.

例如,调用:

printPrice(new Price(10))
printPrice(new Price(9001))
Run Code Online (Sandbox Code Playgroud)

应该打印:

price is 10
price is over 9000
Run Code Online (Sandbox Code Playgroud)

如何printPrice使用模式匹配实现?

scala pattern-matching

7
推荐指数
2
解决办法
2373
查看次数