zoukankan      html  css  js  c++  java
  • 正则表达注意事项(二)(1000则)

    前言

    该节介绍,注意事项。

    正文

    第一个注意事项是:

    ^ 不仅表示开头项,还表示非的意思。

    比如说我们D,表示是非数字。

    我们可以这样写:

    [^0-9] 或者 [^d]表示非的意思。

    第二个是,注意区分w和D,D表示的是非数字,它有很多东西可以匹配。

    w表示的是字母,数字和下划线。

    也就是说w是[_a-z0-9A-Z]的结合体。

    那么W就是[^_a-z0-9A-Z]。

    关于^的一个灵活应用,比如说你想要非w,而且还附加其他条件。

    那么你可以这样[^w@] 这样就不仅非w而且还非@了。

    第三个:s 匹配的不是空格。
    s 包括:

    1. 空格
    2. 制表符( )
      3.换行符( )
      4.回车符( )

    S对应的是[^ ]

    值得注意的是s不包括所有的空白符,空白符还有:
    f 换行符
    h 水平空白符等。。。

    第四个:
    .并是不是全部的字符。 或者 不是。为什么这么说呢?
    匹配除“ ”之外的任何单个字符。要匹配包括“ ”在内的任何字符,请使用像“(.| )”的模式。

    THE RIME OF THE ANCYENT MARINERE, IN SEVEN PARTS.
    
    ARGUMENT.
    
    How a Ship having passed the Line was driven by Storms to the cold
    Country towards the South Pole; and how from thence she made her course
    to the tropical Latitude of the Great Pacific Ocean; and of the strange
    things that befell; and in what manner the Ancyent Marinere came back to
    his own Country.
    
    I.
    
    1      It is an ancyent Marinere,
    2        And he stoppeth one of three:
    3      "By thy long grey beard and thy glittering eye
    4        "Now wherefore stoppest me?
    

    我用 .* 去匹配,得到的结果是:

    共找到 29 处匹配:
    THE RIME OF THE ANCYENT MARINERE, IN SEVEN PARTS.
    
    
    ARGUMENT.
    
    
    How a Ship having passed the Line was driven by Storms to the cold
    

    我并没有贴出全部的,但是可以说明并不是匹配全部的。
    所有我们匹配单词的时候,尽量不要用.,应该是w{7},用w。

    总结

    该文后续持续更新,如果有坑的话。

  • 相关阅读:
    安装sublime text2 for ubuntu
    ruby中Regexp用法
    rvm is not a function的解决方法
    解决启动mongod 时,出现addr already in use错误
    rails中常用的插件
    在数据库中存储层次数据
    Formtastic: Forms Made Crazy Easy for Rails Developers
    rails安全性
    Rails 增加一个模型(model)
    RPC框架实现思路浅析
  • 原文地址:https://www.cnblogs.com/aoximin/p/12741404.html
Copyright © 2011-2022 走看看