我有很多方法在其定义中有样板代码,请看上面的例子.
replace:: Term -> Term -> Formula -> Formula
replace x y (Not f) = Not $ replace x y f
replace x y (And f g) = And (replace x y f) (replace x y g)
replace x y (Or f g) = Or (replace x y f) (replace x y g)
replace x y (Biimp f g) = Biimp (replace x y f) (replace x y g)
replace x y (Imp f g) = Imp (replace x y …Run Code Online (Sandbox Code Playgroud) 我是regex的新手,我想使用python re从下面的以下多行字符串文本中提取电话号码:
Source = """<p><strong>Kuala Lumpur</strong><strong>:</strong> +60 (0)3 2723 7900</p>
<p><strong>Mutiara Damansara:</strong> +60 (0)3 2723 7900</p>
<p><strong>Penang:</strong> + 60 (0)4 255 9000</p>
<h2>Where we are </h2>
<strong> Call us on:</strong> +6 (03) 8924 8686
</p></div><div class="sys_two">
<h3 class="parentSchool">General enquiries</h3><p style="FONT-SIZE: 11px">
<strong> Call us on:</strong> +6 (03) 8924 8000
+ 60 (7) 268-6200 <br />
Fax:<br />
+60 (7) 228-6202<br />
Phone:</strong><strong style="color: #f00">+601-4228-8055</strong>"""
Run Code Online (Sandbox Code Playgroud)
所以当我编译模式时,我应该能够找到使用
phone = re.findall(pattern,source,re.DOTALL)
['+60 (0)3 2723 7900',
'+60 (0)3 2723 7900',
'+ 60 (0)4 255 9000',
'+6 (03) 8924 …Run Code Online (Sandbox Code Playgroud) 我有一个case类Pair(a: Int, b: Int),它代表一对2个整数.为了拥有Pair(2, 5) == Pair(5, 2),我将该equals方法覆盖如下.
override def equals(that: Any): Boolean = that match {
case Corner(c, d) => (a == c && b == d) || (a == d && b == c)
case _ => false
}
Run Code Online (Sandbox Code Playgroud)
现在,平等成立,Pair(2, 5) == Pair(5, 2)返回真实,就像我想要的那样.但是,这在模式匹配时不起作用:
Pair(2, 5) match {
case Pair(5, 2) => print("This is what I want")
case _ => print("But this is what I get")
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我吗?可以/我应该这样做吗?有哪些替代方案?我真的不想case Pair(2, …
由于我对F#的正则表达式实现并不完全满意,因此我希望实现一个所谓的正则表达链.它基本上如下工作:
s将检查给定的字符串,是否与第一个模式匹配.如果是,它应该执行与第一个模式相关的功能.如果没有,则应继续下一个.
我尝试按如下方式实现它:
let RegexMatch ((s : string, c : bool), p : string, f : GroupCollection -> unit) =
if c then
let m = Regex.Match(s, p)
if m.Success then
f m.Groups
(s, false)
else (s, c)
else (s, c)
("my input text", true)
|> RegexMatch("pattern1", fun g -> ...)
|> RegexMatch("pattern2", fun g -> ...)
|> RegexMatch("pattern3", fun g -> ...)
|> .... // more patterns
|> ignore
Run Code Online (Sandbox Code Playgroud)
问题是,这个代码是无效的,因为正向管道运算符似乎没有管道元组或者不喜欢我的实现"设计".
我的问题是:我可以轻松修复上面的代码,还是应该实现其他类型的正则表达链?
Java Pattern该类的标准实现使用递归来实现许多形式的正则表达式(例如,某些运算符,交替).
这种方法导致堆栈溢出问题,输入字符串超过(相对较小)长度,甚至可能不超过1,000个字符,具体取决于所涉及的正则表达式.
一个典型的例子是以下正则表达式,使用交替Data从周围的XML字符串中提取可能的多行元素(命名),该字符串已经提供:
<Data>(?<data>(?:.|\r|\n)+?)</Data>
Run Code Online (Sandbox Code Playgroud)
上面的正则表达式用于Matcher.find()读取"数据"捕获组并按预期工作的方法,直到提供的输入字符串的长度超过1,200个字符左右,在这种情况下,它会导致堆栈溢出.
是否可以重写上述正则表达式以避免堆栈溢出问题?
我是Scala的新手,但有着不错的Java背景.我的问题是在Scala中覆盖equals方法.以下示例来自Scala cookbook:
class Person (name: String, age: Int) {
def canEqual(a: Any) = a.isInstanceOf[Person]
override def equals(that: Any): Boolean =
that match {
case that: Person => that.canEqual(this) && this.hashCode == that.hashCode
case _ => false
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我们需要的原因
that.canEqual(this)
Run Code Online (Sandbox Code Playgroud)
我的理解是,只有"那个"是一个人才会执行该代码.那么,为什么额外调用isInstanceOf?
在Haskell项目上工作,我正在处理FSNotify包中的Event数据类型.所有的构造函数都是:Event
Added FilePath UTCTime
Modified FilePath UTCTime
Removed FilePath UTCTime
Run Code Online (Sandbox Code Playgroud)
在我的代码中,无论类型构造函数如何,我只对FilePath从中提取Event和执行相同的操作感兴趣; 因此,我很想做一个lambda.
不幸的是,当我将case表达式放入lambda以模式匹配所有三种情况时,代码的可读性会降低; FilePath鉴于类型构造函数的相对同质性,是否存在一些内置习惯用法来提取一个表达式而不必手动模式匹配?
我已经尝试在调用该watchDir操作时将此表达式作为匿名函数传递:
wm <- startManager
sw <- watchDir wm "." (\_ -> True) (\(_ f t) -> putStrLn f)
Run Code Online (Sandbox Code Playgroud)
但是,可以预见,lambda模式匹配中的无关值会导致解析错误.
我希望通用算法查找字符串是否包含重复模式,并且字符串的任何部分都不会被重复模式排除.
例如,查看以下示例字符串:
abcabcabc - true
abcabcabcx - false
cucumbercucumber - true
cucumber - false
abaaabaaabaa - true
Run Code Online (Sandbox Code Playgroud)
我看了这个答案,它解决了一些案例的问题,但在例子的情况下会失败cucumber.我需要一些适用于所有情况的东西.
这段代码实现了一个迷你字符串匹配语言 - 它理解两个符号(?=匹配任何字符,*=匹配一个或多个字符).这段代码似乎工作正常,但似乎有点不优雅.
object Re {
def check(search: String, input: String):Boolean = {
search match {
case `input` => true
case x if x.startsWith("?") =>
check(x.stripPrefix("?"), input.tail)
case x if x.startsWith("*") => input match {
case "" => false
case i => check(x.stripPrefix("*"), i.tail) | check(x, i.tail)
}
case _ => false
}
}
}
Run Code Online (Sandbox Code Playgroud)
具体来说,我不喜欢我说x如果x.startswith(某事)然后必须删除那些东西的情况.
scala是否有更惯用的方法来做到这一点?关于Seq匹配器的工作原理,以便我不需要startsWith或stripPrefix.
基于在模式匹配期间防止移动语义,我的理解是当我match对结构进行操作时,如果不使用引用来进行匹配,则结构将被移动,因为它不是基本类型.为了测试这个,我实现了以下内容:
struct Point {
x: i32,
y: i32,
}
let origin = Point { x: 0, y: 0 };
match origin {
Point { x: x1, y: y1 } => println!("({},{})", x1, y1),
}
// to check if the origin has been moved, and it seems not, why ??
match origin {
Point { x: x1, y: y1 } => println!("({},{})", x1, y1),
}
Run Code Online (Sandbox Code Playgroud)
输出是(0,0) (0,0),这意味着原始结构仍然存在.它不应该在第一次之后被移动match吗?
pattern-matching ×10
regex ×4
scala ×3
haskell ×2
equals ×1
f# ×1
java ×1
lambda ×1
matcher ×1
move ×1
operators ×1
parameters ×1
python-2.7 ×1
python-3.x ×1
rust ×1