Scala小白一键重装系统 求助

在 SegmentFault,解决技术问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。
一线的工程师、著名开源项目的作者们,都在这里:
获取验证码
已有账号?
标签:至少1个,最多5个
正则作为一种元语言工具内置在各种编程语言中,特别适合文本处理、数据验证等领域。其优点是代码短小表现力巨大,缺点是难于阅读难于理解几乎无法调试。我作为一个小菜鸟,希望能够摸清正则的门路,顺利记住一些常见的正则需求而不必每次都上网求助。以下是在js实现的正则环境下。
想要自己构造正则解决需求必先学会模仿,而模仿必先认识。面对一串长长的正则符号,我们第一个目的是认识其中每个符号的表达意思,特别是有些符号不止一个字符要知道其是怎么组合的。很重要一点,正则是从左往右解读的。
确定正则范围
在 / / 之间的是正则表达式,第二个/之后的是模式,你直接第一眼看上去就是 第一个/和最后一个/ 以确定正则的范围
因此如果要用 / 你必须转义,我相信你明白转义的意思即 前面加\形成表达 \/,
当然其他你需要表达的元字符都可以用 \来转义,
so,what is 元字符?
简单理解就是被用做正则表达式语法的字符,
好比html中你要 写 \& 就必须转义,只不过html转义方式是换成[实体字符](.cn/tags/html_ref_symbols.html)罢了。
注意:\不代表都是转义字符,诸如\n等都是元字符,想要匹配\n则要\\n
模式只有3种,可多用
g 表示 global全局i 表示 ignore case 忽略大小写m 表示 multiline 多行gi 表示 全局忽略大小写依次类推
很多文本工具的正则不要求你写后面的模式是因为他们一般直接默认gim
理解什么是全局g
不加g 匹配到一个就结束,加g则一直匹配到结尾
正则常用符号分类
符号的具体含义不太理解没关系,毕竟我们第一次用,且不需要记住所有符号啊,我们的目标是拿下常用的,多见几次就明白了
Anchor:匹配位置而非字符
一行或一个串的起始位置
一行或一个串的结束位置
匹配一个字边界,即字与空格间的位置。
匹配与\b相反的位置
Character class:匹配特殊字符集,预定义了一些字符集给你用,你也可以自己构造
匹配除了换行符之外的所有字符
匹配空白符
匹配非空白符
相当于匹配[A-Za-z0-9_],即所有大小写字母,数字,underscore(连字符_).//注意不是-
与\w正好相反
插一句,空白符包括换页、换行、回车、制表(tab是其中一种)
范围:这个应该隶属匹配字符集的Character class,但感觉分开来讲比较好
表示A到Z范围内都可以匹配,跟数学中的用法一致
表示A、Z都行
表示既不是A也不是Z //这是^的第二种用法了,不过别担心,表示反义它必须在[]里
Escaped(转义):
简单理解就是被用做正则表达式语法的字符需要转义,但个人感觉跟多靠积累
记得js中的\uxxxx这种形式吧,这是转义某种unicode字符,具体什么字符要查表,
另外还有\000这种以八进制开头的与\xFF这种16进制的都可以表示同样的东西,
这三者能够表示所有实体符号,且能互换使用。
匹配由x指明的控制字符,x必须[A-Za-z]
表示tab,等价\t
表示回车,等价\r
表示换行,等价\n
不得不跳出来说说win与linux环境下的行尾了,CRLF即rn是在win下,而linux是LF。这也是为什么linux下写代码复制到win下换行都不见了的原因。
group and lookaround
这部分比较难,
Groups allow you to combine a sequence of tokens to operate on them together. Capture groups can be referenced by a backreference and accessed separately in the results.Lookaround lets you match a group without including it in the result.
请再去菜鸟语法看看试着理解上面那段鸟语的意思。
形成一个子串再进行运算
剩下感觉用处不大
反向引用用法
正则: (\w)a\1
源: hah dad bad dab gag gab
结果: hah dad gag
解释: 这个串匹配三个字符,第一个\w的意思看上面,a就是字母a,\1表示引用第一个匹配到子串;所以最终应该是 BAB这种形式,或者 abcacb这种则会把cac标红.
重复/限定符
正则运算符优先级
认识完符号和一些基本的注意点(转义、反向引用、范围、从左到右解读)后,看看
未完待续....
0 收藏&&|&&2
你可能感兴趣的文章
10 收藏,438
7 收藏,825
1 收藏,623
分享到微博?
我要该,理由是:Scala小白 求助【scala吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:971贴子:
Scala小白 求助收藏
本人Scala小白一枚,正在研读Programming in Scala一书、有几个小题请大神相助。对于高人来说可能有点弱智。。。但是请求点拨!1. complete the following definition, so that factTest is the list of integers List(1,2,6,24,120).// You must call the "fact" function (five times) defined above instead of hardcoding the numbers 1,2,4,24,120.val factTest : List[Int] = List (0)// TODO: change "List(0)" to the correct answer.-------------------------------------------------------2. complete the following definition of the Fibonacci function.def fib (n : Int) : Int = {
throw new RuntimeException ("change this 'throw' to the correct answer")}-----------------------------------------------------------------3. declare the identifier "p1" with a pair consisting of// the Int 7 and the String "hello"val p1 : (Int, String) = (7, hello)--------------------------------------------------------------4. declare the identifier "t1" with a triple consisting of// the Int 7, the String "hello", and the Boolean falseval t1 : (Int, String, Boolean) = (0, null, true)----------------------------------------------------5.write a function "split" that takes a pair of an Int and // a String, and returns a pair of a String and an Int (with the values from // the pair passed an argument.
E.g., split (p1) should return ("hello", 7).// You can use "p._1" and "p._2" to access the first and second components// of a pair.def split (p:(Int,String)) : (String,Int) = {
throw new RuntimeException ("change this 'throw' to the correct answer")}----------------------------------------------6. write a function "sum" that takes a list of integers and sums them.// As with all of the exercises in this assignment, your function must be recursive// and MUST NOT use a while loop.def sum (xs : List[Int]) : Int = {
throw new RuntimeException ("change this 'throw' to the correct answer")}---------------------------------------------------7.given the definition of the function "sumTailRecursiveAux" below, complete the definition of the function// "sumTailRecursive" so that it also sums a list of integers.
You must not alter the definition of "sumTailRecursiveAux".
Your// definition for "sumTailRecursive" must call "sumTailRecursiveAux" directly, and must not call "sum"def sumTailRecursiveAux (accumulator : Int, xs : List[Int]) : Int = xs match {
case Nil =& accumulator
case y::ys =& sumTailRecursiveAux (accumulator + y, ys)}def sumTailRecursive (xs : List[Int]) : Int = {
throw new RuntimeException ("change this 'throw' to the correct answer")}----------------------------------------------------8.complete the following definition of the function "max" that finds the maximum integer in a list of integers.// Note that no value can be returned when the list is empty, hence the "NoSuchElementException".def max (xs : List[Int]) : Int = {throw new RuntimeException ("change this 'throw' to the correct answer")}-------------------------------------------------------9.given the definition of the function "maxTailRecursive" below, complete the definition of the function// "maxTailRecursiveAux" so that "maxTailRecursive" also finds the maximum of a list of integers.
You must not alter the// definition of "maxTailRecursive".
Your definition for "maxTailRecursiveAux" must be recursive and not use while loops.def maxTailRecursiveAux (accumulator : Int, xs : List[Int]) : Int = {
throw new RuntimeException ("change this 'throw' to the correct answer")}def maxTailRecursive (xs : List[Int]) : Int = xs match {
case Nil =& throw new RuntimeException ()
case y::ys =& maxTailRecursiveAux (y, ys)}---------------------------------------------10.Write a recursive function "otpu" ("upto" backwards)// that takes two Int parameters "start" and "end" and produces a "List[Int]"// that counts DOWN from "start" to "end" (inclusive at both ends) one at// a time.
If "start & end", the empty list must be returned.def otpu (start : Int, end : Int) : List[Int] = {
throw new RuntimeException ("change this 'throw' to the correct answer")}
中移动资深大数据架构师主讲,项目负责人手把手带你,培养企业急需的大数据人才!真实大数据项目演练,理论结合实战,3个月高强度实战=2年大数据项目开发经验!
现在比你还小白,之前看过,不过忘得一干二净。
1,scala& val factTest = 1 :: 2 :: 4 :: 24 :: 120 ::NilfactTest: List[Int] = List(1, 2, 4, 24, 120)2,scala& def fib(n:Int):Int = n match {
case 0 =& 0
case 1 =& 1
case _ if(n&1)=&fib(n-1) + fib(n-2)
| }fib: (n: Int)Int3,scala& val p1:(Int,String) = (7,"hello")p1: (Int, String) = (7,hello)4,scala& val t1:(Int,String,Boolean) = (0,null,true)t1: (Int, String, Boolean) = (0,null,true)scala& t1.getClass()res0: Class[_ &: (Int, String, Boolean)] = class scala.Tuple3scala& p1.getClass()res1: Class[_ &: (Int, String)] = class scala.Tuple25,scala& def split(p:(Int,String)):(String,Int) = {
| (p._2, p._1)
| }split: (p: (Int, String))(String, Int)scala& split(p1)res2: (String, Int) = (hello,7)6,scala& def sum(xs:List[Int]):Int = xs match {
case x :: Nil =& x
case x :: xy =& x + sum(xy)
//recursive 递归
| }sum: (xs: List[Int])Int7,scala& def sumTailRecursiveAux(accumulator:Int, xs:List[Int]):Int = xs match {
case Nil =& accumulator
case y :: ys =& sumTailRecursiveAux(accumulator + y, ys)
| }sumTailRecursiveAux: (accumulator: Int, xs: List[Int])Intscala& def sumTailRecursive(xs:List[Int]):Int = {
|You typed two blank lines.
Starting a new command.scala& def sumTailRecursive(xs:List[Int]):Int = xs match {
case x :: Nil =& x
case x :: xy =& sumTailRecursiveAux(x, xy)
| }sumTailRecursive: (xs: List[Int])Int
登录百度帐号推荐应用鏂囩珷鍒楄〃
鍗氬?鍒嗙被锛}

我要回帖

更多关于 张小白 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信