-- 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