正则表达式点滴(2)
发布日期:2021-05-14 04:37:35 浏览次数:18 分类:博客文章

本文共 13166 字,大约阅读时间需要 43 分钟。

  1. ���������[���]������������������������������������������������������������������������������������������
  2.  
  • \w���������������������������������(���������������)������������������(���������[a-zA-Z0-9_]),���������������������������
  • \W: ���������������������������������������������������
  • \s���������������������������(���������[\f\n\r\t\v])���
  • \S������������������������������(���������[^\f\n\r\t\v])���
  1. https? ���������������������������������������������������http,���������������https���?���������������������:������������������(s)���������������������������������������������
  2. {}������������������������������������������������������������������������(������������������������������).���������,{3,}������������������3������������������������������������������������3���������������������
  3. ������������������: *?    (������*������������������)���
  4. ������������������������������������������������������

������������������

������������������

*

*?

+

+?

{n,}

{n,}?

  1. ������������

���������cat������������cat,���������������������category,������������������������cat,������������������\bcat\b���������(b���boundary(������)���������),���������������������������������������������������\B.

  1. .*���������������������������(.������������������������������)
  2. ������JavaScript���������������������������:

(?m)^\s*//.*$

���������������

(?m)  ������������

^���$  ������������������������������

\s*  ���������������������������������

//     ������������������

.*    ������������������������������������

  1. ���������������(���),���������������������������������������������������������IP���������������������������������������������������

\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}

���������������

(\d{1,3}.){3}\d{1,3}

������������������������������������999.999.999.999������������������������������������������������IP������������������������������

  • ���������������  ���������������������������(\d{1,2})
  • ������������������������1��� 1\d{2}
  • ������������������������2���2[0-4]\d
  • ������������������������25���25[0-5]

������������������������������������������������

(((\d{1,2})|(1\d{2})|(2[0-4]\d)|( 25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|( 25[0-5]))

  1. ������������

������������������������������������������������������������������������������������������������������������

���������������������

 This is is a text, and I want to know know if there is any repeated words in it.

���������������������������������������

[ ]+(\w+)[ ]+\1

���������������������������������������������������������������

is  is

know know

������������������������������������[ ]+������������������������������\w+������������������������������������������[ ]+���������������������������������\w+���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������\1,���������������������������������������������������������������������������������������������(\w+)���������������is������������\1���������������is������(\w+)���������������know������������\1������������know.

������\1���������������������������������������������������������������������\2���������������������������,\3������������������������������������������������������������������������������������[ ]+(\w+)[ ]+\1���������������������������������������������������

  1. ==============================================================================

������������������

.

������������������������

|

������������������

[]

������������������������������������

[^]

���������������������

-

���������������������������A-Z���

\

������������������������

 

���������������

*

���������������������(������������)������������������������

*?

*������������������

+

���������������������(������������)������������������������

+?

+������������������

?

������������������������(������������)���������������������

{n}

���������������������(������������)���n���������

{m,n}

���������������������(������������)������m������������n���������

{n,}

���������������������(������������)n���������������������

{n,}?

{n,}������������������

 

���������������

^

������������������������

\A

������������������������

$

������������������������

\Z

������������������������

\<

���������������������

\>

���������������������

\b

���������������������������������������

\B

\b���������

 

���������������������

[\b]

������������

\c

������������������������

\d

������������������������

\D

\d���������

\f

���������

\n

���������

\r

���������

\s

������������������������

\S

\s���������

\t

���������(Tab������)

\v

���������������

\w

������������������������������������������������

\W

\w���������

\x

������������������������������

\o

���������������������������

 

���������������������������

()

������������������������

\1

���������������������������,\2���������������������������,������������

?=

������������

?<=

������������

?!

���������������

?<!

���������������

?()

������(if    then)

?()|

������(if    then    else)

 

���������������

\E

������\L������\U���������

\l

���������������������������������

\L

������������������������������������������������\E������

\u

���������������������������������

\U

������������������������������������������������\E������

 

������������

(?m)

������������������

 

  1. ������������

���������������������������������������������������������������?=���������������������������������������������������=������������

������������������������������URL���������http������https,������������������������������

http://www.cnblogs.com

���������������:.+(?=:)

���������: http

������������.+(:) ,������ http:

  1. ������������

������������������������������������������������������(���������������)���������������?<=

������������ABC0:  $12.56

������������  (?<=\$)[0-9.]+

������������?<=������������$12.56,���������12.56

  1. ������������������������

���������������������

<head>

<title>Ben Forta���s HomePage</title>

</head>

���������������������������<title>���</title>������������������������������������<title>���</title>���������������������������������������������������������������������������������������������������������������������������������������������������:

������������������(?<=<title>).*?(?=</title>)

���������������������������������������������������������������������������������������������������������������������������������������������������������������

���������

������

(?=)

���������������

(?!)

���������������

(?<=)

���������������

(?<!)

���������������

 

  1. ���������������

���������:I paid $30 for 100 apples.

������������\b(?<!\$)\d+\b

���������������������������������$������������������������������������������100

���������������������������������������������������������������

  1. ���������������������������������

������������������������������������������������������������������������������������������?������������������������������������������������������

  • ���������������������������������������������
  • ���������������������������������������������

���������������������������������������������������������������������<img>���������������������������������������������������<img>������������������������(������������<A>���</A>������������)������������������������������������������������������

������������������������������������?(backreference)true-regex, ������?���������������������������������������backreference������������������������true-regex���������������backreference���������������������������������������������

     <!-- Nav Bar -->

         <TD>

         <a href='/home'><img src='/images/home.gif'></a>

         <img src=/images/spacer.gif'>

         <a href='/search'><img src='/images/search.gif'></a>

         <img src='/images/spacer.gif'>

         <a href='/help'><img src='/images/help.gif'></a>

         </td>

 

���������������������

(<[Aa]\s+[^>]+>\s*)?<[Ii][Mm][Gg]\s+[^>]+>(?(1)\s*</[Aa]>)

���������������

(<[Aa]\s+[^>]+>\s*)?������������������������������<A>������<a>������

<[Ii][Mm][Gg]\s+[^>]+>������������<img>���������������������������������

(?(1)\s*</[Aa]>)������������������������������������?(1)���������������������������������������(���������������������������<A>������)������������������\s*</[Aa]>������������������(���������������������������������<A>���������������������������������������������������)���������(1)������,\s*</[Aa]>���������������������</A>������������������������������������

���������.NET���������������������

View Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Drawing; namespace RegularExpressionConsoleApplication {     class Program     {                static void Main(string[] args)         {             //LoopBackMatch();  //������������(������������������)             //LoopForwardMatch(); //������������(?=)             //LoopAfterwardMatch(); //������������(?<=)             //LoopForwardAndAfterwardMatch(); //������������������������             ConditionItems(); //���������������������������         }         ///          /// ������������         ///          static void LoopForwardMatch()         {             string text = " http://www.cnblogs.com";             string expression = @".+(?=:)";             Regex reg = new Regex(expression, RegexOptions.IgnoreCase);             MatchCollection mc = reg.Matches(text);             foreach (Match match in mc)             {                 Console.WriteLine(match.Value);             }             Console.ReadKey();         }         ///          /// ������������         ///          static void LoopAfterwardMatch()         {             string text = "ABC01: $12.56";             string expression = @"(?<=\$)[0-9.]+";             Regex reg = new Regex(expression, RegexOptions.IgnoreCase);             MatchCollection mc = reg.Matches(text);             foreach (Match match in mc)             {                 Console.WriteLine(match.Value);             }             Console.ReadKey();         }         ///          /// ������������������������         ///          static void LoopForwardAndAfterwardMatch()         {             StringBuilder sbStr = new StringBuilder();             sbStr.Append("");             sbStr.Append("\r\n ");             sbStr.Append("Ben Forta's HomePage");             sbStr.Append("\r\n");             sbStr.Append("");             string expression = @"(?<=).*?(?=)";             Regex reg = new Regex(expression, RegexOptions.IgnoreCase);             MatchCollection mc = reg.Matches(sbStr.ToString());             foreach (Match match in mc)             {                 Console.WriteLine(match.Value);             }             Console.ReadKey();         }         ///          /// ���������������������������������������         ///          ///          static void LoopBackMatch()         {             string text = " this is  is a text, and I want to know   know if there is any repeated words in it.";             string expression = @"[ ]+(\w+)[ ]+\1";             Regex reg = new Regex(expression, RegexOptions.IgnoreCase);             MatchCollection mc = reg.Matches(text);             foreach (Match match in mc)             {                 Console.WriteLine(match.Value);             }             Console.ReadKey();         }         ///          /// ������������,���������������������:         /*********************************************************          * 
* * * * * * * *********************************************************/ ///
static void ConditionItems() { StringBuilder text = new StringBuilder(); text.Append("
"); text.Append("\r\n"); text.Append(""); text.Append("\r\n"); text.Append(""); text.Append("\r\n"); text.Append(""); text.Append("\r\n"); text.Append(""); text.Append("\r\n"); text.Append(""); text.Append("\r\n"); text.Append(""); text.Append("\r\n"); text.Append(""); string expression = @"(
]+>\s*)?
]+>(?(1)\s*)"; Regex reg = new Regex(expression, RegexOptions.IgnoreCase); MatchCollection mc = reg.Matches(text.ToString()); //foreach (Match match in mc) //{ // Console.WriteLine(match.Value); //} Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("the total count of match result is: " + mc.Count); for (int i = 0; i < mc.Count; i++) { Match match = mc[i]; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("|-Match["+i+"] is :"+match.Value); GroupCollection groups = match.Groups; for (int j = 0; j < groups.Count; j++) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("|---Groups[" + j + "] is :" + groups[j]); } } Console.ReadKey(); } } }

 

 

������������������������������������������

上一篇:EventWaitHandle使用方法小记
下一篇:基于托管C++的增删改查及异步回调小程序

发表评论

最新留言

表示我来过!
[***.240.166.169]2025年04月06日 00时21分45秒