如何在Erlang中进行动态模式匹配?
我有功能过滤器/ 2:
filter(Pattern, Array)
Run Code Online (Sandbox Code Playgroud)
其中Pattern是一个字符串,其中包含我想要匹配的模式(例如"{book, _ }"或"{ebook, _ }"),由用户输入,而Array是异构元素的数组(例如{dvd, "The Godfather" } , {book, "The Hitchhiker's Guide to the Galaxy" }, {dvd, "The Lord of Rings"},等等)然后我想上面的filter/2返回Array中的元素数组匹配模式.
我已经尝试了一些erl_eval没有任何成功的想法......
请提前.
我是Lua正则表达式等价函数的新手,我需要编写以下正则表达式,它应该匹配带小数的数字
\b[0-9]*.\b[0-9]*(?!])
Run Code Online (Sandbox Code Playgroud)
基本上,它匹配十进制格式的数字(例如:1,1.1,0.1,0.11),它们不以']'结尾,我一直试图用Lua使用这样的正则表达式string.gmatch,但我很缺乏经验与Lua匹配表达式...
谢谢!
我有一个数据框,其前5行看起来如下:
Sample CCT6 GAT1 IMD3 PDR3 RIM15
001 0000000000 111111111111111111111 010001000011 0N100111NNNN 01111111111NNNNNN
002 1111111111 111111111111111111000 000000000000 0N100111NNNN 00000000000000000
003 0NNNN00000 000000000000000000000 010001000011 000000000000 11111111111111111
004 000000NNN0 11100111111N111111111 010001000011 111111111111 01111111111000000
005 0111100000 111111111111111111111 111111111111 0N100111NNNN 00000000000000000
Run Code Online (Sandbox Code Playgroud)
完整的数据集有2000个样本.我正在尝试编写代码,这将允许我告诉我所有样本中5列中每列的数字串是否是同质的(即全部为1或0).理想情况下,我还希望能够在答案的情况下区分1和0 True.从我的例子来看,预期结果将是:
Sample CCT6 GAT1 IMD3 PDR3 RIM15
001 TRUE (0) TRUE (1) FALSE FALSE FALSE
002 TRUE (1) FALSE TRUE (0) FALSE TRUE (0)
003 FALSE TRUE (0) FALSE TRUE (0) TRUE (1)
004 FALSE FALSE FALSE TRUE (1) FALSE
005 …Run Code Online (Sandbox Code Playgroud) 如何使用Powershell在文件中找到多行模式,例如XML节点的内容?
即如果我在deviceDescription节点中寻找"绿色"一词,但XML节点文本可能跨越多行,这不起作用:
dir -r -i *.xml | select-string -Pattern "<deviceDescription>.*green.*</deviceDescription>"
Run Code Online (Sandbox Code Playgroud) 我想知道这是否可行.我正在尝试识别数据中存在模式的记录.
例如,包含字段ID,DATA_DTE(仅限每日记录),SPEED的表.
我想确定何时SPEED字段下降至少4并且连续至少连续3天保持不变
**ID**..**DATA_DTE**...**SPEED**
1........Jan-1............8
1........Jan-2............9
1........Jan-3............4
1........Jan-4............4
1........Jan-5............4
1........Jan-6............7
1........Jan-7............8
1........Jan-8............9
Run Code Online (Sandbox Code Playgroud)
从上面,我基本上想要一个SQL查询返回ID.例如,上面的例如"1".
有谁知道我如何设置我的SQL查询来返回这些匹配这样的模式的ID?我将在ms access 2003中运行它.
我希望能够在一个模式中多次使用单个变量,这样只有在所有地方都存在相同的值时它才会匹配,例如
list match {
case x :: x :: xs => // recurse
}
Run Code Online (Sandbox Code Playgroud)
哪个匹配List(1,1,2)但不匹配List(1,2,1).但这不能编译error: x is already defined as value x.
在研究这个问题时,我发现我还可以在案例条款中加入一名警卫,所以我可以这样做
list match {
case x1 :: x2 :: xs if x1==x2 => // recurse
}
Run Code Online (Sandbox Code Playgroud)
它似乎以相同的方式工作(它确实,对吧?).这很好,但是如果我想在很多地方使用相同的值,那就不会那么干净了
list match {
case x1::x2::x3::x4::xs if x1==x2 && x2==x3 && x3==x4 => // recurse
}
Run Code Online (Sandbox Code Playgroud)
有没有更优雅的方式我能做到这一点?
一些注意事项:是的,我只是在学习scala,如果不清楚的话,我不确定这是我真正想做的事情,但我只是对可能的事情感兴趣.在这方面,我真的不想找一个完全不同的解决方案,像takeWhile或filter什么的,但更多的是模式匹配中特别感兴趣.
所以我使用了一个空的数据帧
df=data[['ID','Matrix','Name','Country', 'Units']]
df['Value']=''
Run Code Online (Sandbox Code Playgroud)
我用这样的代码填充它,它找到包含'Good','Bad'值的字符串df.Matrix并用以下值填充它们sch[i]:
df.loc[df.Matrix.str.contains('Good'),'Value'] = sch[2]
df.loc[df.Matrix.str.contains('Bad'),'Value'] = sch[6]
df.loc[df.Matrix.str.contains('Excellent'),'Value'] = sch[8]
Run Code Online (Sandbox Code Playgroud)
我遇到了一堆像这两个不同的错误:
C:\Python33\lib\site-packages\pandas\core\strings.py:184: UserWarning: This pattern has match groups. To actually get the groups, use str.extract.
" groups, use str.extract.", UserWarning)
C:\Users\0\Desktop\python\Sorter.py:57: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
df.loc[df.Matrix.str.contains('Bad'),'Value'] = sch[6]
Run Code Online (Sandbox Code Playgroud)
到目前为止,我正在使用压缩代码
pd.options.mode.chained_assignment = None
Run Code Online (Sandbox Code Playgroud)
如果我不压制错误消息,我将得到大约20个错误消息.是否有其他格式我可以更改数据,以便我不会收到错误消息?
我使用python 3和pandas 0.131如果它有帮助
我正在寻找一种更短/更简洁的方法来完成以下相同的操作(对于编译时已知的任何模式):
let f x = match x with | ['A'::_] -> true ; | _ -> false
Run Code Online (Sandbox Code Playgroud)
是否有一种方法可以执行此操作,即如果表达式与给定模式匹配则返回true?
我有一个案例类层次结构来编码一些请求和处理错误:
sealed trait OpError
sealed trait RequestErrorType
sealed trait ProcessingErrorType
final case class InvalidEndpoint(reason: String) extends RequestErrorType
final case class InvalidParameters(reason: String) extends RequestErrorType
final case class InvalidFormat(response: String) extends ProcessingErrorType
final case class EntityNotFound(id: Long) extends ProcessingErrorType
final case class RequestError(errorType: RequestErrorType) extends OpError
final case class ProcessingError(errorType: ProcessingErrorType) extends OpError
Run Code Online (Sandbox Code Playgroud)
如果我在所有模式中编写简单匹配:
def printMatches(error: OpError): Unit = error match {
case RequestError(InvalidEndpoint(reason)) => //print something
case RequestError(InvalidParameters(reason)) => //print something
case ProcessingError(InvalidFormat(format)) => //print something
case ProcessingError(EntityNotFound(entityId)) => //print …Run Code Online (Sandbox Code Playgroud) Java 21记录模式承诺将解构引入 Java 语言。然而,它似乎与模式匹配紧密耦合,只能用作instanceof比较的一部分或在switch语句/表达式中。
考虑以下记录。
public record Point(int x, int y) {}
Run Code Online (Sandbox Code Playgroud)
instanceof有没有一种方法可以在不使用or 的情况下解构该特定类型的对象switch?以下尝试进行解构point。但如果我们假设空安全,该代码就没有什么意义,因为instanceof既没有也没有必要。switch
Point point = new Point(0, 0);
// destructuring with instanceof
if (point instanceof Point(int x, int y)) {
System.out.printf("Point at (%d,%d)", x, y);
}
// destructuring with switch
switch (point) {
case Point(int x, int y) -> System.out.printf("Point at (%d,%d)", x, y);
}
Run Code Online (Sandbox Code Playgroud)