Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2445439
  • 博文数量: 328
  • 博客积分: 4302
  • 博客等级: 上校
  • 技术积分: 5486
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-01 11:14
个人简介

悲剧,绝对的悲剧,悲剧中的悲剧。

文章分类

全部博文(328)

文章存档

2017年(6)

2016年(18)

2015年(28)

2014年(73)

2013年(62)

2012年(58)

2011年(55)

2010年(28)

分类: Python/Ruby

2012-12-15 13:28:07

Regexp::Common模块提供了一系列基础的正则表达式,可以非常方便有效的完成许多复杂的文本过滤处理任务,下面简单介绍一些例子,详细信息请参考

点击(此处)折叠或打开

  1. # 标准用法

  2.  use Regexp::Common;
  3.  while (<>) {
  4.      /$RE{num}{real}/ and print q{a number};
  5.      /$RE{quoted}/ and print q{a ['"`] quoted string};
  6.      /$RE{delimited}{-delim=>'/'}/ and print q{a /.../ sequence};
  7.      /$RE{balanced}{-parens=>'()'}/ and print q{balanced parentheses};
  8.      /$RE{profanity}/ and print q{a #*@%-ing word};
  9.  }

  10.  # 函数接口用法
  11.  use Regexp::Common 'RE_ALL';
  12.  while (<>) {
  13.      $_ =~ RE_num_real() and print q{a number};
  14.      $_ =~ RE_quoted() and print q{a ['"`] quoted string};
  15.      $_ =~ RE_delimited(-delim=>'/') and print q{a /.../ sequence};
  16.      $_ =~ RE_balanced(-parens=>'()'} and print q{balanced parentheses};
  17.      $_ =~ RE_profanity() and print q{a #*@%-ing word};
  18.  }

  19.  # 内联用法
  20.  if ( $RE{num}{int}->matches($text) ) {...}


  21.  # ...AND SUBSTITUTION
  22.  my $cropped = $RE{ws}{crop}->subs($uncropped);


  23.  # 自定义
  24.  use Regexp::Common 'pattern';

  25.  pattern name => ['name', 'mine'],
  26.          create => '(?i:J[.]?\s+A[.]?\s+Perl-Hacker)',
  27.          ;
  28.  my $name_matcher = $RE{name}{mine};

  29.  pattern name => [ 'lineof', '-char=_' ],
  30.          create => sub {
  31.                         my $flags = shift;
  32.                         my $char = quotemeta $flags->{-char};
  33.                         return '(?:^$char+$)';
  34.                     },
  35.          match => sub {
  36.                         my ($self, $str) = @_;
  37.                         return $str !~ /[^$self->{flags}{-char}]/;
  38.                     },
  39.          subs => sub {
  40.                         my ($self, $str, $replacement) = @_;
  41.                         $_[1] =~ s/^$self->{flags}{-char}+$//g;
  42.                    },
  43.          ;

  44.  my $asterisks = $RE{lineof}{-char=>'*'};

  45.  # DECIDING WHICH PATTERNS TO LOAD.
  46.  use Regexp::Common qw /comment number/; # Comment and number patterns.
  47.  use Regexp::Common qw /no_defaults/; # Don't load any patterns.
  48.  use Regexp::Common qw /!delimited/; # All, but delimited patterns.

子模块Regexp::Common::comment用法

  1.     use Regexp::Common qw /comment/;
  2.     while (<>) {
  3.         /$RE{comment}{C}/ and print "Contains a C comment\n";
  4.         /$RE{comment}{C++}/ and print "Contains a C++ comment\n";
  5.         /$RE{comment}{PHP}/ and print "Contains a PHP comment\n";
  6.         /$RE{comment}{Java}/ and print "Contains a Java comment\n";
  7.         /$RE{comment}{Perl}/ and print "Contains a Perl comment\n";
  8.         /$RE{comment}{awk}/ and print "Contains an awk comment\n";
  9.         /$RE{comment}{HTML}/ and print "Contains an HTML comment\n";
  10.     }

  11.     use Regexp::Common qw /comment RE_comment_HTML/;
  12.     while (<>) {
  13.         $_ =~ RE_comment_HTML() and print "Contains an HTML comment\n";
  14.     }

  15.     # 获取代码里面的注释或者非注释
  16.     my $string = read_file($fileName) ;
  17.     my $comment =~ m/$RE{comment}{Java}/g;
  18.     my $code =~ s/$RE{comment}{Java}//g;
阅读(3050) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~