Perl Study Notes No. 9

Source: Internet
Author: User

Use regular expressions to process files

1. S //, S/ABC/Fred/first matches ABC. If ABC is matched, use Fred to replace ABC. If ABC is not matched, do nothing.

2. S // G, replace globally, S/ABC/Fred/g, replace all ABC with Fred

3. Other symbols can be used to replace/as the Separator

4./six also applies

5. $ _ = "I saw Barney with Fred .";
$ _ = S/(Fred | Barney) // U $1/GI; # "I saw Barney with Fred ."

All uppercase letters behind/U. Similarly, all lowercase letters behind/L. to terminate the effect, use/e.

6./L and/u are only for the next character

7./u/L, indicating that the first letter is in upper case and the other is in lower case

8. The above modifier can also be used in "" of the print statement.

9. Split data that cannot be separated by commas (,)... 0.0

10. split compares the mode with the string and returns the substrings separated by delimiters as the list. for example, @ fields = Split/:/, "ABC: Def: G: hij ";

11. If two delimiters appear consecutively, an empty element is returned, but the ending empty element is discarded.

12. by default, the split operation is performed on $ _, and the mode is blank. my @ fields = split; # It is equivalent to split/S +/, $ _; but there is also a difference, that is, it will discard the empty element at the beginning

13. Join function, my $ x = join ":", "4, 6, 8, 10, 12"; # $ X is 4: 6: 8: 10: 12

14. If the second parameter has only one or no elements, only one or null is returned.

15. Remember that the first parameter of join is a string, not a mode.

16. In the context of the list, the matching expression also returns the list

My $ text = "I love you! I love my city! ";

My @ result = ($ text = ~ /[A-Z] +/Gi );

Print "@ result/N"; # output: I love you I love my city

17. If there are more than one parentheses, more than one string will be returned each time. For example, put the string in the hash:

My $ DATA = "Barney Rubble Fred Flintstone Wilma Flintstone ";

My % last_name = ($ DATA = ~ /(/W +)/S + (/W)/g );

Each time the pattern match is successful, a pair of values are returned to become the key/value of the hash.

18. Non-Greedy quantifiers... + ,*,?, {} Are all greedy quantifiers, that is, they are eaten first and then vomit (rolled back). Non-Greedy ones are matched first and then eaten. The specific efficiency is actually related to data.

19. + ?, *?, {}?,?? Is all non-Greedy quantifiers

20./M modifier, used to process multi-line strings (with multiple line breaks)

21. $ ^ I this scalar value can determine the type of the while (<>) backup file, such as $ ^ I = ". Bak"; or $ ^ I = "~ ";

22. Add? :, Indicates that this bracket is only grouped and does not cause memory variable allocation.

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.