Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Regular expressions in Scala and sample Analysis of pattern matching

Shulou Source: shulou.com Published: 2022-06-02 06:12:57 09月24日 Update

This article mainly introduces regular expressions in Scala and sample analysis of pattern matching. The introduction in this article is very detailed and has certain reference value. Interested friends must read it!

regular expression

//"" Native expression val regex="""([0-9]+)([a-z]+)"".r val numPattern="[0-9]+".r val numberPattern=""\s+[0-9]+\s+"".r

Description: .r() Method Introduction: Scala converts strings to regular expressions

/** You can follow a string with `.r`, turning it into a `Regex`. E.g. * * `"""A\w*""".r` is the regular expression for identifiers starting with `A`. */ def r: Regex = r()

Pattern match one.

//findAllIn() method returns iterator over all matches for(matchString s"$x$y" * }).toList // List(hath, hatth, hattth, hatttt) * }}} * * Attempting to retrieve match information before performing the first match * or after exhausting the iterator results in [[java.lang.IllegalStateException]]. * See [[scala.util.matching.Regex.MatchIterator]] for details. * * @param source The text to match against. * @return A [[scala.util.matching.Regex.MatchIterator]] of matched substrings. * @example {{{for (words println(num+"\t"+blog) case _=>println("hahaha... ") }

val line="93459h spark" line match{ case numitemPattern(num,blog)=> println(num+"\t"+blog) case _=>println("hahaha... ") }

All program source code in this section

package kmust.hjr.learningScala19/** * Created by Administrator on 2015/10/17. */object RegularExpressOps { def main(args:Array[String]):Unit={ val regex=""([0-9]+)([a-z]+)"".r//"" Native expression val numPattern="[0-9]+".r val numberPattern=""\s+[0-9]+\s+"".r //findAllIn() method returns iterator for(matchString println(num+"\t"+blog) case _=>println("hahaha... ") } }}

The above is all the content of this article "Regular expressions in Scala and sample analysis of pattern matching", thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Tags: Patterns regularities expressions methods examples analysis content letters numbers introduction articles combinations iterations lieutenant generals values interests functions characters strings boys Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Redmi Docker Linux Huawei