首先,介绍一下背景。我有类似于以下内容的字符串:
((Foo.Bar.StartsWith("A")) && (Foo.Bar.EndsWith("B")))
我正在尝试将这些重新格式化为如下所示:
((Foo.Any(Bar.StartsWith("A"))) && (Foo.Any(Bar.EndsWith("B"))))
旁注: .Bar 后面的部分有时可能不包含 (),例如 .Bar == "AB" 。
我试图捕捉两个群体:Foo。和 Bar.StartsWith("<A or B>") 。我制定了以下模式,但它删除了尾随括号。
\((Foo\.)(.*?)\)
Run Code Online (Sandbox Code Playgroud)
因此,我得到的不是我想要的,而是:
((Foo.Any(Bar.StartsWith("A")) && (Foo.Any(Bar.EndsWith("B")))
这是两个括号短。
问题是我需要在 .* 中包含右括号?如果那里还有一个左括号,则匹配。我还没想好怎么做,有人知道吗?
PS - 代码是 C#.NET 但这应该无关紧要,除非解决方案涉及一些晦涩的语言特定的 RegEx 内容。
我正在尝试匹配字符串中的单引号,如果存在,我想使用WRITE.
data text type string value `this is a string containing a ' single quote`.
find regex `(\'|%27)` in text.
if sy-subrc = 0.
write 'found'.
endif.
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我不了解 ABAP 正则表达式中反引号字符的行为。并且无法在网上找到资源并解释其工作原理。我使用它的结果很奇怪,这取决于匹配项上方的 TEXT 字符串是有效还是失败。
在 PERL 中,您可以执行诸如在字符串中搜索字符串变量之类的操作,例如/:
data text type string value `this is a string containing a ' single quote`.
find regex `(\'|%27)` in text.
if sy-subrc = 0.
write 'found'.
endif.
Run Code Online (Sandbox Code Playgroud)
这种方法可以在 ABAP 中使用吗,或者有人可以解释如何在 ABAP 中使用反引号?
我想在 Haskell 模式匹配中用一个表达式匹配多个条件。
例如,
data Message = HELLO | HI | GOODBYE | BYE
greeting x = case x of
HELLO or HI -> "hello"
GOODBYE or BYE -> "bye"
Run Code Online (Sandbox Code Playgroud)
但我找不到如何做到这一点。
对不起我糟糕的英语。谢谢你。
我被困在 Windows 批处理(cmd)模式搜索中。我需要在文件中搜索模式并需要返回行号。我使用了FINDSTRwith/X选项,但它也将模式匹配的行附加到行号。
此外,我没有安装任何实用程序(如 unix-utilities)的特权,以便我可以cut用来提取行号。
是否可以使用string.match并返回捕获,直到最后一个.字符停止在扩展部分?
我正在尝试为我的数据中的不同种族/民族创建一个指标变量。在我的数据 ('mydata') 中,我有一个名为“Race”的变量。这个变量有一个人在问卷中被标记为种族的每个框的输出。所以它看起来像这样:
ID Race
6 American Indian or Alaska Native, Black or African American, Hispanic or Latino
7 Hispanic or Latino
10 Native Hawaiian or Other Pacific Islander
11 Hispanic or Latino, White
29 White
30 Black or African American
31 American Indian or Alaska Native, Hispanic or Latino, White
Run Code Online (Sandbox Code Playgroud)
我想创建一个变量,以便例如如果有人说他们是西班牙裔,而忽略他们所说的其他内容,在新的“西班牙裔”变量下,他们会得到一个“1”,或者如果他们没有说他们是西班牙裔,那么“西班牙裔” " 会得到 0。
我知道这需要使用部分字符串匹配,但我很难获得我想要的输出。我做了多次尝试,但运气不佳。这是我最后一次尝试的代码:
if(mydata[grep("Hispanic", mydata$Race)]) {
Hispanic<-1
} else {
Hispanic<-0
}
Run Code Online (Sandbox Code Playgroud) fun sample(list_of_str_lists, s) =
case list_of_str_lists of
[] => []
| x::[] => case (all_except_option(s, x)) of
SOME lst => lst
| NONE => []
| x::xs' => case (all_except_option(s, x)) of
SOME lst => lst @ sample(xs', s)
| NONE => [] @ sample(xs', s)
Run Code Online (Sandbox Code Playgroud)
它使用一个辅助函数,该函数接受一个字符串列表,list option如果该列表中有匹配的字符串(同样,不包括匹配的字符串),则返回该列表中的所有元素。所以辅助函数将接受一个列表["a", "b", "c"]和一个字符串"a",如果匹配"a",将返回一个包含["b", “c”].
我的问题出在sample最后一个模式匹配分支中。它抛出错误
earlier rule(s): 'Z list option -> 'Z list
this rule: string list list -> string list …Run Code Online (Sandbox Code Playgroud) 我正在处理表单验证并希望使用模式属性来验证输入字段。该字段具有以下条件:
那么确切的模式应该是什么RegEx。我正在尝试使用以下代码,但如何实现百分比条件?
<input type="text"
pattern="[0-9]+([\.][0-9]{0,2})?"
title="This must be a number with up to 2 decimal places and/or %">
Run Code Online (Sandbox Code Playgroud) 我正在编写一段代码,该代码是一个函数,返回一个像素列表,这是我定义的一种特殊数据类型,其类型如下:
data Pixel = Pixel {
color :: Float,
elevation :: Float,
distance: Float,
visited :: Bool
} deriving (Show)
Run Code Online (Sandbox Code Playgroud)
我具有要通过两个文件并将它们放到一个像素列表中的功能。这些文件分别包含颜色和高程,因此我继续获取此代码输出:
insertPixels :: [String] -> [String] -> [Pixel]
insertPixels _ _ = []
insertPixels (x:xs) (y:ys) = [Pixel (read x) (read y) 99999999 False] ++ insertPixels xs ys
Run Code Online (Sandbox Code Playgroud)
当我尝试对此进行编译或将其加载到解释器中时,出现以下错误:
lab1.hs:33:1: warning: [-Woverlapping-patterns]
Pattern match is redundant
In an equation for ‘insertPixels’:
insertPixels (x : xs) (y : ys) = ...
|
| insertPixels (x:xs) (y:ys) = [Pixel (read x) (read …Run Code Online (Sandbox Code Playgroud) 在使用even()它时,它仅匹配unapply和使用时的真实条件even,这仍然如何打印"Is even"
object even{
def unapply(arg: Int): Boolean = (arg%2 == 0)
}
object singleDigit{
def unapply(arg: Int): Option[Boolean] = {
if(arg < 10) Some(true)
else None
}
}
val n = 9
n match {
case even() => println("Is even")
case singleDigit => println("is single")
case _ => println("No property")
}
//This gives "is single"
n match {
case even => println("Is even")
case singleDigit => println("is single")
case _ => println("No …Run Code Online (Sandbox Code Playgroud)