Matching start position \ G

Source: Internet
Author: User

-- Start

\ G is usually used in iteration operations to indicate the end position of the last match or the start position of the current match. At the first iteration, \ G matches the start of the string. If the expression cannot be matched successfully during the iteration, \ G points to the start position of the string again. However, if we specify the/C modifier of Perl, \ G will not be reset even if the regular expression fails to match. The following is a simple example.

#! /Usr/bin/perlmy $ testtext = "I love regular expression."; # test text while (not $ testtext = ~ M/\ G \ Z/GC) # end after reaching the end of the text {if ($ testtext = ~ M/\ G (REG \ W + \ B)/GC) # search for words starting with Reg {print "$1 \ n"; # reverse reference, $1 Reference Matching content in brackets} elsif ($ testtext = ~ M/\ G (exp \ W + \ B)/GC) # Find the words starting with exp {print "$1 \ n";} elsif ($ testtext = ~ M/\ G (.)/GC) # Skip a character. If this branch does not exist, the loop is endless. {print "$1 \ n ";}}

Although the above example has no practical value, we can easily expand it. We can read the test text from the file, multiple if branches can analyze the file and print the result to the new file.

--For more information, see:Regular Expressions
--Shengming: reprinted, please indicate the source
-- Last updated on 2012-05-12
-- Written by shangbo on 2012-05-12
-- End

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.