加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Learning Perl: 8.1. Matches with m//

发布时间:2020-12-15 20:55:59 所属栏目:大数据 来源:网络整理
导读:? 8.1. Matches with m// We've been writing patterns in pairs of forward slashes,like /fred/ . This is a shortcut for the m// (pattern match) operator. As you saw with the qw// operator,you may choose any pair of delimiters to quote the con

Previous Page

Next Page

?

8.1. Matches with m//

We've been writing patterns in pairs of forward slashes,like /fred/. This is a shortcut for the m// (pattern match) operator. As you saw with the qw// operator,you may choose any pair of delimiters to quote the contents. So,you could write that same expression as m(fred),m<fred>,m{fred},or m[fred] using those paired delimiters,or as m,fred,,m!fred!,m^fred^,or many other ways using nonpaired delimiters. [*]

[*] Nonpaired delimiters are the ones that don't have a different "left" and "right" variety; the same punctuation mark is used for both ends.

The shortcut is that if you choose the forward slash as the delimiter,you may omit the initial m. Since Perl folks love to avoid typing extra characters,you'll see most pattern matches written using slashes,as in /fred/.

Choose a delimiter that doesn't appear in your pattern.[

] If you wanted to make a pattern to match the beginning of an ordinary web URL,you might write /http:///// to match the initial "http://". But that'll be easier to read,write,maintain,and debug if you use a better choice of delimiter: m%http://%.[

]
It's common to use curly braces as the delimiter. If you use a programmer's text editor,it probably has the ability to jump from an opening curly brace to the corresponding closing one,which can be handy in maintaining code.

[

] If you're using paired delimiters,generally you shouldn't have to worry about using the delimiter inside the pattern since that delimiter generally will be paired inside your pattern. That is,m(fred(.*)barney) and m{/w{2,}} and m[wilma[/n /t]+betty] are all fine even though the pattern contains the quoting character,since each "left" has a corresponding "right." But the angle brackets (< and >) aren't regular expression metacharacters,so they may not be paired. If the pattern were m{(/d+)/s*>=?/s*(/d+)},quoting it with angle brackets would mean having to backslash the greater-than sign so that it wouldn't prematurely end the pattern.

[

] Remember,the forward slash is not a metacharacter,so you don't need to escape it when it's not the delimiter.

Previous Page

Next Page

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读