zoukankan      html  css  js  c++  java
  • 正则表达式(实践篇)

    Java

    Pattern (java.util.regex.Pattern)

    类 java.util.regex.Pattern 简称 Pattern, 是Java正则表达式API中的主要入口,无论何时,需要使用正则表达式,从Pattern 类开始

    String text    =
            "This is the text to be searched " +
            "for occurrences of the pattern.";
    String pattern = ".*is.*";
    boolean matches = Pattern.matches(pattern, text);
    System.out.println("matches = " + matches);

    上面代码在变量 text 中查找单词 “is” 是否出现,允许”is” 前后包含 0或多个字符(由 .* 指定)
    Pattern.matches() 方法适用于检查 一个模式在一个文本中出现一次的情况。

    如果需要匹配多次出现,甚至输出不同的匹配文本,或者只是需要非默认设置。需要通过Pattern.compile() 方法得到一个Pattern 实例。

    String text    =
            "This is the text to be searched " +
            "for occurrences of the http:// pattern.";
    String patternString = ".*http://.*";
    Pattern pattern = Pattern.compile(patternString);

    可以在Compile 方法中,指定一个特殊标志:
    Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
    Pattern 类包含多个标志(int 类型),这些标志可以控制Pattern 匹配模式的方式。上面代码中的标志使模式匹配是忽略大小写。

    一旦获得了Pattern对象,接着可以获得Matcher对象。Matcher 示例用于匹配文本中的模式.示例如下
    Matcher matcher = pattern.matcher(text);

    Matcher类有一个matches()方法,可以检查文本是否匹配模式。以下是关于Matcher的一个完整例子

    String text    =
            "This is the text to be searched " +
            "for occurrences of the http:// pattern.";
    String patternString = ".*http://.*";
    Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(text);
    boolean matches = matcher.matches();
    System.out.println("matches = " + matches);

    首先创建一个Pattern,然后得到Matcher ,调用matches() 方法,返回true 表示模式匹配,返回false表示不匹配。
    可以用Matcher 做更多的事。

    matches() 方法不能用于查找正则表达式多次出现。如果需要,请使用find(), start() 和 end() 方法。

    find() 方法用于在文本中查找出现的正则表达式,文本是创建Matcher时,通过 Pattern.matcher(text) 方法传入的。如果在文本中多次匹配,find() 方法返回第一个,之后每次调用 find() 都会返回下一个。

    start() 和 end() 返回每次匹配的字串在整个文本中的开始和结束位置。实际上, end() 返回的是字符串末尾的后一位,这样,可以在把 start() 和 end() 的返回值直接用在String.substring() 里。

    String text    =
            "This is the text which is to be searched " +
            "for occurrences of the word 'is'.";
    String patternString = "is";
    Pattern pattern = Pattern.compile(patternString);
    Matcher matcher = pattern.matcher(text);
    int count = 0;
    while(matcher.find()) {
        count++;
        System.out.println("found: " + count + " : "  + matcher.start() + " - " + matcher.end());
    }
     输出结果:
    found: 1 : 2 - 4 found: 2 : 5 - 7 found: 3 : 23 - 25 found: 4 : 70 - 72

    Javascript

    RegExp 对象

    直接创建

    /pattern/attributes

    构造方法创建

    new RegExp(pattern, attributes);

    参数
    pattern 是一个字符串,指定了正则表达式的模式或其他正则表达式。
    attributes 是一个可选的字符串,包含属性 "g"、"i" 和 "m",分别用于指定全局匹配、区分大小写的匹配和多行匹配。ECMAScript 标准化之前,不支持 m 属性。如果 pattern 是正则表达式,而不是字符串,则必须省略该参数。

    RegExp 对象有 3 个方法:test()、exec() 以及 compile()。

    test() 方法检索字符串中的指定值。返回值是 true 或 false。

    var patt1=new RegExp("e");
    document.write(patt1.test("The best things in life are free")); 
    由于该字符串中存在字母
    "e",以上代码的输出将是: true

    exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。

    var patt1=new RegExp("e");
    document.write(patt1.exec("The best things in life are free")); 
    由于该字符串中存在字母
    "e",以上代码的输出将是: e

    compile() 方法用于改变 RegExp。

    compile() 既可以改变检索模式,也可以添加或删除第二个参数。

    var patt1=new RegExp("e");
    document.write(patt1.test("The best things in life are free"));
    patt1.compile("d");
    document.write(patt1.test("The best things in life are free"));
    
    由于字符串中存在 "e",而没有 "d",以上代码的输出是:
    truefalse
  • 相关阅读:
    Attention中的qkv与机器翻译中的对应关系
    numpy.copy和torch.tensor的cpu/gpu
    F1值的优化macro
    py中函数是传值还是传引用
    scipy.optimize.minimize||非线性规划
    离散label的优化trick
    python Queue/collections.deque
    tf.pad学习
    tensor2tensor-transformer源码学习
    最早的attention论文学习-通过联合学习进行对齐和翻译的神经机器翻译
  • 原文地址:https://www.cnblogs.com/luoxiaolei/p/6718447.html
Copyright © 2011-2022 走看看