我想通过在Scala中使用正则表达式从字符串中提取子字符串。
例如,字符串
line = "[129]: (29) -> (1): Rumor"
我想从行中提取三个数字和最后一个子字符串("Rumor"
)。
如何在scala中实现它?
更新
谢谢@Shyamendra Solanki,它现在有效。但我有另一个问题。当我从文件中读取一行并使用正则表达式解析此行时,eclipse报告奇怪的错误(我不知道什么是错的)。但如果我不使用它,则不会报告错误。
val pattern = """\[(\d+)\]:\s\((\d+)\)\s\W\W\s\((\d)\):\s(\w+)""".r // define regex
for(line: String <- Source.fromFile(f1).getLines()){
val pattern(clock, from, to, msg) = line
println(clock+", "+ from+", "+to+", msg")
}
错误:
Exception in thread "AWT-EventQueue-0" scala.MatchError: [1]: (1) <- (0): Init([LNode;@1f18e146,Full@1a6dca9d) (of class java.lang.String)
at TwoNodeQuery$$anon$1$$anonfun$1$$anonfun$apply$mcV$sp$2.apply(Analysis.scala:74)
at TwoNodeQuery$$anon$1$$anonfun$1$$anonfun$apply$mcV$sp$2.apply(Analysis.scala:73)
at scala.collection.Iterator$class.foreach(Iterator.scala:727)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1157)
at TwoNodeQuery$$anon$1$$anonfun$1.apply$mcV$sp(Analysis.scala:73)
at scala.swing.Action$$anon$2.apply(Action.scala:60)
at scala.swing.Action$$anon$1.actionPerformed(Action.scala:78)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3312)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
答案 0 :(得分:2)
val re = """\[(\d+)\]:\s\((\d+)\)\s\W\W\s\((\d)\):\s(\S+)""".r // define regex
val s = "[129]: (29) -> (1): Rumor" // line to match
val re(n1, n2, n3, s1) = s // pattern match and extract values
(n1.toInt, n2.toInt, n3.toInt, s1) // convert to expected types
答案 1 :(得分:0)
从Scala 2.13
开始,可以按unapplying a string interpolator模式匹配String
s:
val s"[$n1]: ($n2) -> ($n3): $rumor" = "[129]: (29) -> (1): Rumor"
n1: String = 129
n2: String = 29
n3: String = 1
rumor: String = Rumor
请注意,我们也可以在提取器内的{em> 中使用regex
。
因此,为了强制前三个数字为有效整数:
val Nbr = "(\\d+)".r
"[129]: (29) -> (1): Rumor" match {
case s"[${Nbr(n1)}]: (${Nbr(n2)}) -> (${Nbr(n3)}): $rumor" =>
Some(n1.toInt, n2.toInt, n3.toInt, rumor)
case _ => None
}
// Option[(Int, Int, Int, String)] = Some((129,29,1,Rumor))