想象一下,我们有一个包含子串'cat'和'dog'以及其他随机字符的长字符串,例如.
cat x dog cat x cat x dog x dog x cat x dog x cat
Run Code Online (Sandbox Code Playgroud)
这里'x'代表任何随机的字符序列(但不是'cat'或'dog').
我想要做的是找到除"狗"之外的所有字符后面的每个"猫",然后是"猫".我想在每种情况下删除第一个'cat'实例.
在这种情况下,我想删除括号[cat],因为在下一个'cat'之前没有'dog':
cat x dog [cat] x cat x dog x dog x cat x dog x cat
Run Code Online (Sandbox Code Playgroud)
最终得到:
cat x dog x cat x dog x dog x cat x dog x cat
Run Code Online (Sandbox Code Playgroud)
如何才能做到这一点?
我想以某种方式使用类似(N)(?=(n))的正则表达式作为VonC推荐 这里
(cat)(?=(.*cat))
Run Code Online (Sandbox Code Playgroud)
匹配字符串中的所有'cat'对.但是我仍然不确定如何使用它来移除每只在'cat'之前没有跟随'dog'的猫.
我正在处理的真正问题是Java.但我真的只是在寻找一般的伪代码/正则表达式解决方案.
尝试这个新的stackoverflow事情,正如建议:)这不是真正的haskell特定,但它在haskell中最清楚.
这是一个偶然出现的模式:一个函数需要两个对称处理的参数.mappends经常有这个属性.一个例子:
-- | Merge sorted lists of ranges.
merge :: (Ord n) => [(n, n)] -> [(n, n)] -> [(n, n)]
merge [] r2 = r2
merge r1 [] = r1
merge r1@((s1, e1) : rest1) r2@((s2, e2) : rest2)
| e1 < s2 = (s1, e1) : merge rest1 r2
| e2 < s1 = (s2, e2) : merge r1 rest2
| s1 >= s2 && e1 <= e2 = merge rest1 r2 -- 1 within 2
| …Run Code Online (Sandbox Code Playgroud) 我正在使用jQuery并且我正在尝试编写模式替换,但它不起作用.我有这个:
var $featured_rewrite = $('#featured').not('.slideshow');
$featured_rewrite.children().attr('href', $featured_rewrite.find('img').attr('src').replace('/-[0-9]+x[0-9]+\./i', '.'));
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这样的事情有效:
.replace('-500x277.', '.')
Run Code Online (Sandbox Code Playgroud)
但不是这个,我甚至用工具检查过并确保它有效且有效:
.replace('/-[0-9]+x[0-9]+\./i', '.')
Run Code Online (Sandbox Code Playgroud) 当下面的代码无法编译时,我有点惊讶:
-- Code 1
import Complex
type Velocity = Complex Double
type Force = Complex Double
type FrictionCoeff = Double
frictionForce :: FrictionCoeff -> Velocity -> Force
frictionForce mu vel = mu * vel
Run Code Online (Sandbox Code Playgroud)
错误说
Couldn't match expected type `Complex Double'
with actual type `Double'
Expected type: Force
Actual type: FrictionCoeff
In the first argument of `(*)', namely `mu'
In the expression: mu * vel
Run Code Online (Sandbox Code Playgroud)
所以,简而言之
-- Code 2
let z = 1 :+ 2
z * 3 -- Goes …Run Code Online (Sandbox Code Playgroud) 我是OCaml的新手并且阅读了Real World OCaml(RWO)一书.模式匹配在第3章中描述,与Erlang(或Prolog)的模式匹配相比似乎很弱.
我的问题是:
一个具体的例子:
以下功能(取自RWO第63页)降级列表
let rec destutter list =
match list with
| [] -> []
| [hd] -> [hd]
| hd :: hd' :: tl ->
if hd = hd' then ds1 (hd' :: tl)
else hd :: ds1 (hd' :: tl)
;;
# destutter [1;2;3;3;4;5;5;6];;
- : int list = [1; 2; 3; 4; 5; 6]
Run Code Online (Sandbox Code Playgroud)
在Erlang中,有可能(我认为首选)使用模式匹配而不是条件:
destutter([]) -> [];
destutter([X]) -> [X];
destutter([H,H|T]) -> destutter([H|T]);
destutter([H|T]) -> [H | destutter(T)].
Run Code Online (Sandbox Code Playgroud)
在OCaml中尝试这种事情......
let rec …Run Code Online (Sandbox Code Playgroud) 我正在通过学习你的haskell教程,我一直在嘲笑作者给出的一些例子.
例如,他重新实现zip如下:
zip' :: [a] -> [b] -> [(a,b)]
zip' _ [] = []
zip' [] _ = []
zip' (x:xs) (y:ys) = (x,y):zip' xs ys
Run Code Online (Sandbox Code Playgroud)
他对所有其他例子采用了类似的方法,他将最具体的模式放在第一位.这是一个略有不同的zip函数版本:
zip' :: [a] -> [b] -> [(a,b)]
zip' (x:xs) (y:ys) = (x, y):zip' xs ys
zip' _ _ = []
Run Code Online (Sandbox Code Playgroud)
据我所知,两种方法都做同样的事情.如果提供空列表(x:xs)或(y:ys)将不匹配,将通过附加空列表[]来完成递归.
亲切的问候,
编辑:
可能重复: Haskell GHC:模式与N个构造函数匹配的时间复杂度是多少?
简介:模式的顺序对于语义(在严格评估参数方面)和函数的可读性非常重要.模式匹配本身将始终处于O(1)时间复杂度.
我尝试在python中使用nltk实现一个正则表达式标记生成器,但结果如下:
>>> import nltk
>>> text = 'That U.S.A. poster-print costs $12.40...'
>>> pattern = r'''(?x) # set flag to allow verbose regexps
... ([A-Z]\.)+ # abbreviations, e.g. U.S.A.
... | \w+(-\w+)* # words with optional internal hyphens
... | \$?\d+(\.\d+)?%? # currency and percentages, e.g. $12.40, 82%
... | \.\.\. # ellipsis
... | [][.,;"'?():-_`] # these are separate tokens; includes ], [
... '''
>>> nltk.regexp_tokenize(text, pattern)
[('', '', ''), ('', '', ''), ('', '-print', ''), ('', '', …Run Code Online (Sandbox Code Playgroud) 请注意,我已阅读此帖,但该帖子使用了switch语句,当模式匹配时,它应该执行某些操作(返回true).另一方面,如果模式匹配并使用if-case语句,我不想做任何事情.
我有这个枚举:
enum MyEnum {
case a
case b(Int)
case c
case d
}
Run Code Online (Sandbox Code Playgroud)
这是它的一个实例:
let myEnum: MyEnum = .a
Run Code Online (Sandbox Code Playgroud)
现在我想要做一些事情,如果myEnum不是.b.由于.b有一个关联的值,我不能简单地使用if语句检查:
if myEnum != .b { // compiler error
// do my thing here
}
Run Code Online (Sandbox Code Playgroud)
所以我必须使用if-case语句来模式匹配它:
if case .b(_) = myEnum {
} else {
// do my thing here
}
Run Code Online (Sandbox Code Playgroud)
但我真的很讨厌使用空if子句.这对我来说只是看起来不够.我试图天真地这样做:
if case .b(_) != myEnum { // compiler error!
// do my thing here
}
Run Code Online (Sandbox Code Playgroud)
除了使用空的if子句之外,还有更好的方法吗?
无论模式是否匹配,我仍然有应该运行的代码,因此guard语句将不起作用.
我正在研究一些F#并试图像本教程中那样实现一个解析器组合器; 在对提议的解决方案进行一些复制和粘贴之后,我尝试自己进行自定义.
当然有一些我错过了,但编译器给了我一个奇怪的错误信息.
type T<'a> =
| A of string
| B of 'a
let foo a b =
match a with
| A s as x -> x
| B i ->
match b with
| A s as x -> x
| B j ->
B (i, j)
Run Code Online (Sandbox Code Playgroud)
上面的代码是我发现的问题的概括:在最后的结果(最里面的匹配表达式的B分支)中通知错误:
error FS0001: Type mismatch. Expecting a
'a
but given a
'a * 'b
The resulting type would be infinite when unifying ''a' and ''a * 'b'
Run Code Online (Sandbox Code Playgroud)
但如果我不使用该 …
我正在尝试编写一个自定义模式匹配,让我打开错误并匹配错误代码.请参阅以下示例:
enum ErrorCode: Int {
case notSoDumb
case dumbError
}
let myError = NSError(domain: "My domain", code: ErrorCode.dumbError.rawValue, userInfo: nil)
func ~=(pattern: ErrorCode, value: NSError) -> Bool {
return (ErrorCode(rawValue: value.code) == pattern)
}
switch myError {
case ErrorCode.notSoDumb:
print("Not a dumb error")
case ErrorCode.dumbError:
print("Super dumb error")
default:
print("No matches!")
}
Run Code Online (Sandbox Code Playgroud)
我的switch语句中的第一个案例有错误Enum case 'notSoDumb' is not a member of type 'NSError'.如果我ErrorCode用整数替换枚举(并更新我的自定义~=运算符以匹配Ints和NSErrors,一切正常.