Perl Language Learning note 9 Regular expression processing text

Source: Internet
Author: User
Tags processing text

1. Replacement

s/pattern/replace/; #返回是否更换成功布尔值

Ability to use capture variables, such as: s/(\w)/$1/

The match fails, no matter what the deal is.

2. Delimiter

For delimiters that have no left or right points. Repeated three times can be, such as: s///, s###

For delimiters with left and right points, you need to use two pairs, one pair includes the pattern, the other includes the replacement string, and the pair can be different. such as: s{}{}, s[]{}, s<>[]

3. Optional modifiers

/g can be replaced globally, replacing all matching strings, such as: s///g

/s: Make. Match all characters

/I: Uppercase and lowercase irrelevant

4. Binding operator

$file _name =~ s#^.*# #s; #去除所哟Unix风格的路径

5. Escape operator

\u: All capitalization, or end to \e

\l: All lowercase, or end to \e

\u: A subsequent character to capitalize

\l: Subsequent eligibility character to lowercase

Able to use, \u\l all to lowercase, capitalize first letter

The above operators can be used in double-cited

6. Split string: Split

@fileds = Split/:/, "ABS:SDF:FDD"; #返回结果列表

Two cut connect prompt together, split empty field. At the end of the meeting is omitted.

The/\s+/is capable of making blank cuts. such as: Split/\s+/, $some _input; #全部的空白都当作一个空格处理, omit empty fields at the beginning

7. String Connection: Join

My $x = join ":", 4,6,8,10; #结果为 $x = "4:6:8:10";

Combine with split to split first. Then connect with a different delimiter.

8. m//in the context of a list

When using m//in the context of a list, assuming the match succeeds, the list of all captured variables is returned. Match failure returns an empty list: my ($first, $second, $third) =/(\s+) (\s+) (\s+)/;

The/g modifier is capable of matching m//to many places. Such as:

9. Non-greedy quantifiers

By default. /fred.+barney/will match "Fred" first. Then match all the remaining strings to ". +". Then ". +" spits out a character in turn. Until "Barney" is matched. This is greedy mode.

In +, *, {},?

Then add "?". Turns it into a non-greedy mode. From less to more matching;

/m: Pattern matching across rows, followed by matching line breaks in rows;

10. Update multiple files at once

$ ^i: Represents the suffix name of the backup file, which will be backed up before processing and read from the backup file. Then create a file with the same name as the source file and write the new information;

11. Online editing with the command line

-P: Take the initiative to generate a small program, while Loop

-i.bak: Backup file name

-W: Open warning

-E: Tell the program that the code is behind

Fred*.dat: Files to work with


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Perl Language Learning note 9 Regular expression processing text

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.