【Perl】Perl语言正则表达式初识

本文详细介绍了Perl语言中的正则表达式及其应用,包括元字符、特殊符号和常见用例,帮助开发者掌握正则表达式的使用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Perl语言提供的正则表达式应用非常广,事实上很多shell命令也支持Perl正则表达式,比如grep、sed等。 

Perl 正则表达式的元字符:

 

.                   a single character
    \s                  a whitespace character (space, tab, newline, ...)
    \S                  non-whitespace character
    \d                  a digit (0-9)
    \D                  a non-digit
    \w                  a word character (a-z, A-Z, 0-9, _)
    \W                  a non-word character
    [aeiou]             matches a single character in the given set
    [^aeiou]            matches a single character outside the given set
    [0-9]               matches all of the digits
    [a-z]               matches all of the low-case letters
    [^0-9]              matches all of the non-digits
    [^a-z]              matches all of the non-low-case letters
    (foo|bar|baz)       matches any of the alternatives specified
    ^                   start of string
    $                   end of string

 

其他元字符:

 

 

*                   zero or more of the previous thing
    +                   one or more of the previous thing
    ?                   zero or one of the previous thing
    {3}                 matches exactly 3 of the previous thing
    {3,6}               matches between 3 and 6 of the previous thing
    {3,}                matches 3 or more of the previous thing

 

简单例子:

 

 

/^\d+/              string starts with one or more digits
    /^$/                nothing in the string (start and end are adjacent)
    /(\d\s){3}/         a three digits, each followed by a whitespace
                        character (eg "3 4 5 ")
    /(a.)+/             matches a string in which every odd-numbered letter
                        is a (eg "abacadaf")
    # This loop reads from STDIN, and prints non-blank lines:
    while (<>) {
        next if /^$/;
        print;
    }

 

 

 

注:

 

\d 匹配一个数字的字符,和 [0-9] 语法一样

 

 

\d+ 匹配多个数字字符串,和 [0-9]+ 语法一样

 

 

\D 非数字,其他同 \d

 

 

\D+ 非数字,其他同 \d+ 

 

 

\w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样

 

 

\w+ 和 [a-zA-Z0-9]+ 语法一样

 

 

\W 非英文字母或数字的字符串,和 [^a-zA-Z0-9] 语法一样

 

 

\W+ 和 [^a-zA-Z0-9]+ 语法一样

 

 

\s 空格,和 [\n\t\r\f] 语法一样

 

 

\s+ 和 [\n\t\r\f]+ 一样

 

 

\S 非空格,和 [^\n\t\r\f] 语法一样

 

 

\S+ 和 [^\n\t\r\f]+ 语法一样

 

 

\b 匹配以英文字母,数字为边界的字符串

 

 

\B 匹配不以英文字母,数值为边界的字符串

 

 

(pattern) () 这个符号会记住所找寻到的字符串,是一个很实用的语法。第一个 () 内所找到的字符串变成 $1 这个变量或是 \1 变量,第二个 () 内所找到的字符串变成 $2 这个变量或是 \2 变量,以此类推下去。  

/pattern/i i 这个参数表示忽略英文大小写,也就是在匹配字符串的时候,不考虑英文的大小写问题。 

\ 如果要在 pattern 模式中找寻一个特殊字符,如 "*",则要在这个字符前加上 \ 符号,这样才会让特殊字符失效

 

补充一些工作中用到的

[^/]+ 匹配除'/'外的任意一个或多个字符

 

参考资料:

http://perldoc.perl.org/perlintro.html#Regular-expressions

转载于:https://my.oschina.net/renhc/blog/54846

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值