• 正则表达式语法


    例子:
    使用python
    import re 
    # lookaround  
    str = '1234567890'  
    regex = re.compile('(?<=\d)(?=(\d\d\d)+$)')  
    str = regex.sub(',', str)  
    print str 
    # backreference  
    str = 'section{First} section{second}'  
    regex = re.compile('section{ ( [^}]* ) }', re.VERBOSE)  
    str = regex.sub(r'subsection{\1}', str)  
    print str 
    # *  
    str ='abxd'  
    regex = re.compile('x*')  
    str = regex.sub('-', str)  
    print str 
    # raw string  
    str = r'\n'  
    print str 
    # greedy and !greedy  
    str = '<html><head>'  
    print re.match('<.*>', str).group()  
    print re.match('<.*?>', str).group()

    使用perl
    #!/usr/bin/perl  
    undef $/;   # Enter "file-slurp" mode  
    $text = <>; # Slurp up the first file given on the command line.   
    $text =~ s/&/&/g;       # Make the basic HTML . . .  
    $text =~ s/</</g;       # . . . characters &, <, and > . . .  
    $text =~ s/>/>/g;       # . . . HTML safe.  
    $text =~ s/ / /g;       # handle blankspace  
    $text =~ s/(?=\n)/<br \/>/g;    # handle \n  
    $hostnameregex = qr/[-a-z0-9]+(\.[-a-z0-9]+)*\.(com|edu|cn)/i;  
    # Turn email addresses into links . . .  
    $text =~ s{  
       \b  
       # Capture the address to $1 . . .  
       (  
         \w[-.\w]*          # username  
         \@  
         $hostnameregex # hostname  
       )  
       \b  
    }{<a href="mailto:$1">$1</a>}gix;   
    # Turn HTTP URLs into links . . .  
    $text =~ s{  
       \b  
       # Capture the URL to $1 . . .  
       (  
         http:// $hostnameregex \b      # hostname  
         (  
           / [-a-z0-9_:\@&?=+,.!/~*'%\$]*   # Optional path  
             (?<![.,?!])            # Not allowed to end with [.,?!]  
         )?  
       )  
    }{<a href="$1">$1</a>}gix;   
    print $text;     # Finally, display the HTML-ized text.  

    使用VB
    ...
    使用C#
    using System.Text.RegularExpressions;
    Match m = Regex.Match(str, @"<\$Loop\$>(.*?)<\$LoopEnd\$>", RegexOptions.IgnoreCase);
    if (m.Success)
    {
        MessageBox.Show(m.Result("$1"));
    }
    javascript(匹配1-100以内的数字)

    <html>
    <body>

    <script language='javascript'>
    function check()
    {
    var aa=document.getElementById('textbox');
    var ValidateExp = /^([1-9][0-9]|[1-9]|100)$/g;
    //var ValidateExp = /^([1-100])$/g;//ERROR

    //  if(!aa.value.match(ValidateExp))  //可以
    if(!/^([1-9][0-9]|[1-9]|100)$/g.test(aa.value))
     {
             
          lblValBP.innerText = "*";
          lblValBP.style.color="red";
          alert("请输入1-100");
          return false;
     }
           
      else
      {
          lblValBP.innerText = "√";
          lblValBP.style.color="green";
          return true;
      }//end else
    }//end function
    </script>

    <form name='myfrom'>
    <p id='lblValBP'></p>
    <input type='text' id='textbox' >
    <input type='button' value='提交' onclick='check()'>
    </form>
    </body>
    </html>

    regexp教程:
    http://wiki.ubuntu.org.cn/Python%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%93%8D%E4%BD%9C%E6%8C%87%E5%8D%97#.E7.BC.96.E8.AF.91.E6.A0.87.E5.BF.97


     

    http://community.csdn.net/Expert/topic/5763/5763178.xml?temp=.3836481
    求一个超难的正则表达式
    -0113--0-310-1
    -1101-1-10--00
    10-1--1--30000
    3--3110-00-1-3
    301--10-1-00-1

    条件1: 长度为14个字符
    条件2: 其中任意9个位置为数字,并且数字只能是(0,1,3)
    条件3: 其余的位置全部为"-"符号

    ------------------------------------------ 求一个正则表达式
    有点意思,问题本身不难,要说有点难度那是在思路上,先给答案,慢慢说思路

    ^(?!(.*?-){6,})(?!(.*?\d){10,})[013-]{14}$
    可以这样拆开来理解:

    1:^和$分别是开始符和结束符;
    2:(?!(.*?-){6,}) 表示不匹配有大于等于6个'-'的对象
    3:(?!(.*?\d){10,}) 表示不匹配有大于等于10个数字的对象
    4:[013-]{14} 表示只能含有这些字符,0,1,3,-,并且长度为14个
    好像这样写也可以哈

    ^(?=(.*?-){5})(?=(.*?\d){9})[013-]{14}$
    --------------------
    这样写在.NET中没问题,但不通用,在验证控件或javascript中不可以
    原因主要是javascript对(?=Exp)支持不稳定,但对(?!Exp)的支持没问题

    ^(?!(.*?-){6,})(?!(.*?\d){10,})[013-]{14}$,在服务器端和客户端都通过;
    ^(?=(.*?-){5})(?=(.*?\d){9})[013-]{14}$,在服务器端验证通过,客户端失败;
    ^(?=(.*?-){5})[013-]{14}$,在服务器和客户端都测试失败

    其实看不出这道题目有多高级。而且这类型的题目从理论上来说,应该是正则比较慢。

    无论是.NET还是JS都可以用一个简单的方法来解决:

    1、判断字符串长度是否为14位。
    2、s = s.Replace( "-", "" );//JS有类似方法
    3、判断Replace后的字符串是否为9位。
    4、判断这个9位的字符串是否全部由0、1、3组成。无论是遍历还是其他方法,都很简单。


    当然,正则的代码更简洁。不过,代码却让VMM一眼之下无法理解,何况没有效率上的优势。孰优孰劣,大家自行判断。

    正则表达式之道
    原著:Steve Mansour
    sman@scruznet.com
    Revised: June 5, 1999
    (copied by jm /at/ jmason.org from http://www.scruz.net/%7esman/regexp.htm, after the original disappeared! )

    翻译:Neo Lee

    http://net.pku.edu.cn/~yhf/tao_regexps_zh.html

    正则表达式30分钟入门教程
    版本:v2.21 (2007-8-3) 作者:deerchao 来源:unibetter大学生社区 转载请注明来源

    目录
    本文目标
    如何使用本教程
    正则表达式到底是什么?
    入门
    测试正则表达式
    元字符
    字符转义
    重复
    字符类
    反义
    替换
    分组
    后向引用
    零宽断言
    负向零宽断言
    注释
    贪婪与懒惰
    处理选项
    平衡组/递归匹配
    还有些什么东西没提到
    联系作者
    一些我认为你可能已经知道的术语的参考
    网上的资源及本文参考文献
    更新说明
    http://www.unibetter.com/deerchao/zhengzhe-biaodashi-jiaocheng-se.htm

  • 相关阅读:
    kvm
    Javascript 笔记与总结(2-7)对象
    [Swift]LeetCode172. 阶乘后的零 | Factorial Trailing Zeroes
    Swift5.3 语言指南(五) 基本运算符
    C#6.0语言规范(一) 介绍
    [Swift]LeetCode171. Excel表列序号 | Excel Sheet Column Number
    [Swift]LeetCode169. 求众数 | Majority Element
    [Swift]LeetCode168. Excel表列名称 | Excel Sheet Column Title
    [Swift]LeetCode167. 两数之和 II
    [Java]LeetCode141. 环形链表 | Linked List Cycle
  • 原文地址:https://www.cnblogs.com/cutepig/p/894253.html
Copyright © 2020-2023  润新知