Getting started with Scala to mastering--12th section I/O vs. regular expressions

Source: Internet
Author: User

The main contents of this section
    1. Introduction to Scala I/O operations
    2. Scala Writing Files
    3. Scala Read files
    4. Scala Network I/O
    5. Introduction to Regular expressions
    6. Scala regular-expression combat
1. Introduction to Scala I/O operations

I/O operations are an important part of a programming language, and in Scala it is more about calling I/O classes in Java or implementing I/O operations by encapsulating the I/O classes in Java. We have used the I/O operation in the previous section:

trait FileLogger extends Logger{  val fileName:String  //PrintWriter使用的是java.io.PrintWriter类  val fileOutput=new PrintWriter(fileName:String)  fileOutput.println("#")  def log(msg:String):Unit={    fileOutput.print(msg)    fileOutput.flush()    }}

Scala's own I/O content is relatively small, giving the Scala I/O-related classes

As you can see, the number of I/O related classes in the Scala language is much smaller compared to the I/O classes in the Java language, the most common of which is the source class, so it's important to learn more about I/O-related classes in Java by learning Scala I/O operations.

2. Scala writes files

Scala writes files directly using the I/O classes in Java, such as

import java.io._object ScalaFileWriter {  def main(args: Array[String]): Unit = {    val fileWriter=new FileWriter("file.txt")    fileWriter.write("scala file writer")    fileWriter.flush()    fileWriter.close()  }}

You can see that the file writes in Scala are no different from the I/O operations in Java, which shows that Scala can interoperate with Java.

3. Read the file

Scala can read files directly using the I/O classes in Java, or using the source object in Scala, which encapsulates I/O in Java, is easier and more flexible to use, and the following code gives you the ability to read the file and format the output of the file content:

import scala.io._object ScalaFileReader {  def main(args: Array[String]): Unit = {    //读取文件    val file=Source.fromFile("D:\\scala\\doc\\api\\package.html")    //返回Iterator[String]    val lines=file.getLines()    //打印内容    for(i<- lines) println(i)      //关闭文件    file.close();  }}
4. Network I/O

Network I/O operations in Scala can be implemented either through the Fromurl method in the source object, or using native Java network I/O operations, and the following code demonstrates the network I/O reading in Scala. Baidu home Page content:

importimport scala.io.Source.fromURLobject NetworkIO {  def main(args: Array[String]): Unit = {    print(fromURL(new URL("http://www.baidu.com")).mkString)  }}
5 Introduction to Regular expressions

In many programming languages, including Java, Perl, PHP, Python, JavaScript, and JScript, there is no exception to the native support for regular expression processing. The Scala language also supports regular expressions, and, of course, Scala can use regular expressions directly from Java to manipulate regular expressions, but Scala implements its own way, which is more flexible. It is worth mentioning that the regular expression is not only a language of the program, it is beyond the limits of a language or a system, become a widely used tool.
In the program development, often encounter need to match, find, replace, judge the string, if it is solved by the pure coding method, it is very difficult, and it is a waste of time, through regular expression can solve these problems. The meanings of the symbols commonly used in regular expressions are given below:
1 period symbol. It is a wildcard that matches a character, such as SPA.K, to a string of arbitrary letters, such as Spark, Spaak, or a string of special characters such as Spa#k,spa K
2 [], qualifying matches, for example Spa[ark]k will only match spark,spaak,spakk three strings, for the other does not match
3 |, or matching, such as Spa (A|r|rr|k) K, can match Spark,spaak,spakk and Sparrk
4 $, matching line terminator, for exampleSpark$Matches are in theSpark$For the end line, for example I love spark, but it does not match spark would be very poupular in the future
5 ^, match line start character, for example ^spark matches lines starting with spark, such as Spark will is very poupular in the future, does not match I love Spark
6 *, matches 0 characters, for exampleSpar*That can match any spar starting string, such as Spar,spark,sparkkkkk
7/, escape character, for exampleSpark/$Matches a string that contains spark$
8 (), a group character, which saves the matching content in () and can be accessed, such as Spa (A|r|rr|k) K can save the match in () as a temporary variable that can be accessed directly in the program
9 +, matched one or more times for exampleSpar+That can match any spar starting string, such as Spark,sparkkkkk
10, Match 0 or one time, for exampleSpark(s)?Can match spark and sparks
{n}, matching n times, e.g. spark{2}, can match the sparkk in I love SPARKK
{n,}, matches at least n times, for example Spark{2,} can match sparksss and sparkss in I love Sparksss sparkss
@ {n,m}, match at least N times, up to M times, sparks{2,4} can match sparkssss in I love Sparks sparkssss

The use of the qualifying match [] is very flexible and it is necessary to further explain it:

[a-Z] conditions are restricted to lowercaseaA character in the to Z range [A-Z] conditions are limited to uppercaseAA character in the to Z range [a-ZA-Z] conditions are limited to lowercaseato Z or uppercaseAA character in the to Z range [0-9] conditions are limited to lowercase0To9A character in the range [0-9a-Z] conditions are restricted to lowercase0To9OraA character in the to Z range [0-9[a-Z]] condition restricted to lowercase0To9OraA character (intersection) ^ symbol in the to Z range has another meaning in qualifying match [], which is the inverse action [^a-Z] conditions are restricted to non-lowercaseaA character in the to Z range [^A-Z] conditions are restricted to non-uppercaseAA character in the to Z range [^a-ZA-Z] conditions are restricted to non-lowercaseato Z or uppercaseAA character in the to Z range [^0-9] conditions are restricted to non-lowercase0To9A character in the range [^0-9a-Z] conditions are restricted to non-lowercase0To9OraOne character in the to Z range

Other special characters:
\ back Slash
\ t interval ('/u0009 ')
\ n line break ('/u000a ')
\ r Enter ('/u000d ')
\d number equivalent to [0-9]
\d non-numeric equivalent to [^0-9]
\s blank symbol [/T/N/X0B/F/R]
\s non-blank symbol [^/T/N/X0B/F/R]
\w individual characters [a-za-z_0-9]
\w non-individual characters [^a-za-z_0-9]
\f Page Break
\e Escape
\b The boundary of a word
\b A non-word boundary
\g the end of a previous match

6 Common Regular Expression combat

Here are some of the most commonly used regular expression operations to appreciate the charm of regular expressions and how it is used in Scala.
1 Matching mailboxes:

object RegexMatch {  def main(args: Array[String]): Unit = {    val sparkRegex="^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$".r    for(matchString <- sparkRegex.findAllIn("[email protected]"))    {      println(matchString)    }  }}

2 Matching URLs:

object RegexMatch {  def main(args: Array[String]): Unit = {    val sparkRegex="^[a-zA-Z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\s*)?$".r    for(matchString <- sparkRegex.findAllIn("http://www.xuetuwuyou.com"))    {      println(matchString)    }  }}

3 Matching mobile phone number:

object RegexMatch {  def main(args: Array[String]): Unit = {    val sparkRegex="(86)*0*13\\d{9}".r    for(matchString <- sparkRegex.findAllIn("13887888888"))    {      println(matchString)    }  }}

4 Matching IP address:

object RegexMatch {  def main(args: Array[String]): Unit = {    val sparkRegex="(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)".r    for(matchString <- sparkRegex.findAllIn("192.168.1.1"))    {      println(matchString)    }  }}

In Scala, there's a very, very powerful feature, which is the extractor (Extractor), which we'll talk about in a few words about the Scala extractor, this section simply shows how the extractor has been used with regular expressions in Scala.

Extract matching IP address sub-segments:

object RegexMatch {  def main(args: Array[String]): Unit = {    val ipRegex="(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)".r    for(ipRegex(one,two,three,four) <- ipRegex.findAllIn("192.168.1.1"))    {      println("IP子段1:"+one)      println("IP子段2:"+two)      println("IP子段3:"+three)      println("IP子段4:"+four)    }  }}

To extract a user name from a mailbox:

object RegexMatch {  def main(args: Array[String]): Unit = {    val sparkRegex="^([\\w-]+(\\.[\\w-]+)*)@[\\w-]+(\\.[\\w-]+)+$".r    for(sparkRegex(domainName,_*) <- sparkRegex.findAllIn("[email protected]"))    {      println(domainName)    }  }}

Add a public number to find out more about the latest spark and Scala technical information

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Getting started with Scala to mastering--12th section I/O vs. regular expressions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.