Perl learning notes 9 Regular Expression Processing text

Source: Internet
Author: User
Tags processing text

1. Replacement

S/pattern/replace/; # returns the Boolean value of whether the replacement is successful.

You can use capture variables, such as S/(\ W)/$1/

If the matching fails, no processing is performed.

2. delimiters

For delimiters without left or right, repeat them three times, for example, S //, s ###

There are two pairs of delimiters with left and right points. One pair of include mode and one pair of replace strings can be different. For example: s {}{}, s [] {}, S <> []

3. Optional Modifier

/G can be replaced globally, replacing all matched strings, such as: S // G

/S: Make. Match All characters

/I: case-insensitive

4. Bind Operators

$ File_name = ~ S # ^. * # s; # Remove all UNIX paths

5. Escape Operators

\ U: Convert all to uppercase, or end with \ e

\ L: Convert all to lowercase, or end with \ e

\ U: The next character is converted to uppercase

\ L: the subsequent qualification characters are converted to lowercase letters

Can be used together, \ U \ L all to lowercase, the first letter is capitalized

The preceding operators can be used in double quotation marks.

6. Split string: Split

@ Fileds = Split/:/, "ABS: SDF: FDD"; # Return result list

When two delimiters are connected together, the fields are split out, and the ending part is omitted.

/\ S +/can be blank, such as: Split/\ s +/, $ some_input; # All blank spaces are treated as a space, omitting the blank fields at the beginning

7. String concatenation: Join

My $ x = join ":",; # The result is $ x = "4: 6: 8: 10 ";

Combined with split, split first and connect with different delimiters.

8. m in the list context //

When M // is used in the context of the list, if the matching succeeds, the list of all captured variables is returned. If the matching fails, an empty list is returned: My ($ first, $ second, $ third) =/(\ s + )/;

The/g modifier can match M // to many places, such:

9. Non-Greedy quantifiers

By default,/Fred. + Barney/matches "Fred" first, and then matches all the remaining strings as ". + ", and then". + "one character is spit out until" Barney "is matched. This is greedy mode.

In + ,*,{},? Add "?", Changes it to a non-Greedy mode, from less to more matching;

/M: Cross-row mode match. After this parameter is added, the line break in the row will be matched;

10. Update multiple files at a time

$ ^ I: indicates the suffix of the backup file. Before the backup is processed, the file is backed up, read from the backup file, and create a file with the same name as the source file to write new information;

11. Online Editing Using command lines

-P: automatically generates a small program, while loop

-I. Bak: Backup File Name

-W: open warning

-E: Tell the program the code behind it.

Fred *. DAT: file to be processed


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.