`
sillycat
  • 浏览: 2542915 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Perl Language(IV)Regular and Control

阅读更多
Perl Language(IV)Regular and Control

7. Regular statement(regex)
$string =~ m/$patten/
$string =~ m{$patten}
$string =~ m|$patten|
$string =~ m!$patten!
$string =~ /$patten/

my $answer = "monger";
until ((my $patten = <STDIN>) =~ /$answer/) {   
    print "wrong\n";
};

my $answer = "mo*r"; 
while (1) {   
    if ((my $patten = <STDIN>) =~ /$answer/) { 
        print "*match*\n";
    } else {
        print "*not match*\n";
    }
};

my $answer = "mo{2,4}r";

my $answer = "(wow)+";

8. More Regex
/f(oo|ee)t/    # foot or feet
/on (March|April|May)/

$content =~ /[pP]erl/;

$content =~ /perl/i;     # charactor 'i' is for ignore the Capital letter

my $content = "I like perl I am a perl monger";
if ($content =~/I like(.*)monger/) {
    print "*$1*\n";
}

We can get all the charactors between 'like' and 'monger'

8.7. Compare and Replace
my $content = "I love Java";
print $content if ($content =~ s/java/perl/i);

console output:
I love perl

s is for replacing. i is for ignoring the Capital charactors.

my $content = "I love Perl. I am a perl monger";
print $content if ($content =~ s/perl/Perl/gi);

console output:
I love Perl. I am a Perl monger

g is for all.

8.8. Replacing in our own string
my $string = "123456";
print $string if ($string =~ s/(1)(2)(3)(4)(.*)/$3$2$1$4$5/);

console output:
321456

9. Flow Control
9.1.1 last
break the loop.

for (1...10) {
    last if ($_ ==;
    print $_;
}

console output:
1234567

9.1.2 redo
redo the things in loop
for (1...10) {
    $_++; 
    redo if ($_ ==;
    print $_ . " ";      
}

console output:
2 3 4 5 6 7 9 9 10 11

9.1.3 next
omit the current loop
for (1...10) {
    next if ($_%2); 
    print $_ . " ";
}

console output:
2 4 6 8 10


references:
http://easun.org/perl/perl-toc/
http://perl.apache.org/
分享到:
评论

相关推荐

    PCRE(Perl Compatible Regular Expressions)

    PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正规表达式库.这些在执行正规表达式模式匹配时用与Perl 5同样的语法和语义是很有用的。Boost太庞大了,使用boost regex后,程序的编译速度...

    Perl Language Reference Manual

    Perl语言参考手册是Perl编程语言的权威文档,提供了关于Perl版本5.12.1的详细信息,这本手册由Perl的创始人Larry Wall和团队编写,并由Network Theory Ltd出版。Perl作为一种高级、通用、解释型、动态编程语言,自...

    Perl-compatible Regular Expressions VC compile project

    The PCRE library is a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl, with just a few differences. Certain features that appeared in ...

    The Perl Programming Language

    - **逻辑运算**:包括AND、OR、NOT等。 - **字符串操作**:如连接、替换、截取等。 - **数组操作**:如排序、插入、删除等。 ##### 3.3 控制流程 Perl支持多种控制结构,如条件语句(if-else)、循环语句(while、...

    pcre(Perl Compatible Regular Expressions)库源代码

    PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。这些在执行正规表达式模式匹配时用与Perl 5同样的语法和语义是很有用的。

    Perl--Regular expression.pdf

    my $str = "I love Perl and Perl is great"; $str =~ s/Perl/Python/g; print $str; # 输出 "I love Python and Python is great" ``` - **表达式替换 `e`**:将替换部分视为一个表达式来执行。 ```perl my ...

    Perl Best Practices: Standards and Styles for Developing Maintainable Code

    This is especially critical in Perl, because the language is designed to offer many ways to accomplish the same task, and consequently it supports many incompatible dialects. With a good dose of ...

    An Introduction to Perl Regular Expressions in SAS

    在本文档中,我们要讨论的是Perl正则表达式在SAS中的应用,即在SAS软件中如何使用Perl风格的正则表达式来进行模式匹配和字符串操作。 首先,要了解SAS(Statistical Analysis System)是统计分析和商业智能的软件...

    Modern.Perl.4th.Ed

    You'll understand how the language fits together and discover the secrets used by the global Perl community. This beloved guide is now completely updated for Perl 5.22. When you have to solve a ...

    perl regular expressions

    在Perl中,正则表达式(Regular Expression,简称regex)被广泛用于数据验证、文本处理和文件操作等场景。 1. 正则表达式基本语法: - 定界符:`/pattern/`,通常用斜线 `/` 来包围正则表达式,但也可以选择其他非...

    perl Language

    它的名称“Perl”最初是“Practical Extraction and Reporting Language”的首字母缩写,但后来被理解为“Pretty Excellent Reporting Language”或“Pathologically Eclectic Rubbish Lister”。Perl因其强大的文本...

    beginning perl for bioinformatics

    Regular expressions and finding motifs in data, Arrays, hashes, and relational databases, Regular expressions and restriction maps, Using Perl to parse PDB records, annotations in GenBank, and BLAST ...

    Apress.Beginning.Perl.3rd.Edition.Apr.2010

    Commencing with a comprehensive overview of language basics, you’ll learn all about important concepts such as Perl’s data types and control flow constructs. This material sets the stage for a ...

    Perl language

    ### Perl语言:编程界的多面手 #### 概览与历史背景 Perl语言,自诞生以来,便以其独特的魅力在编程领域占据了一席之地。它最初由Larry Wall于1987年创建,旨在解决系统管理和文本处理中的复杂问题。随着版本4在...

    PERL REGULAR EXPRESSIONS QUICK START

    ### PERL 正则表达式快速入门 #### 简介 正则表达式是 Perl 编程语言中一个非常强大的工具,它可以帮助开发者轻松地处理字符串数据,进行模式匹配、搜索、替换等操作。本篇文章将从简单的单词匹配开始,逐步深入...

    pcre 8.30-Perl Compatible Regular Expressions

    “pcre 8.30-Perl Compatible Regular Expressions”是指一个特定版本(8.30)的Perl兼容正则表达式库。这个库是为那些需要在自己的应用程序中实现Perl风格正则表达式功能的开发者设计的。 **描述详解:** Perl ...

    Beginning Perl for Bioinformatics

    Regular expressions and finding motifs in data, Arrays, hashes, and relational databases, Regular expressions and restriction maps, Using Perl to parse PDB records, annotations in GenBank, and BLAST ...

    minimal_perl_for_unix_and_linux_people.pdf

    In this preface, I’ll tell you about the history of Minimal Perl and the origins of this book. THE HISTORY OF MINIMAL PERL The seeds of this book were sown many years ago, when I was building up my ...

Global site tag (gtag.js) - Google Analytics