(Prints "1") print "Letter: $let\n";#-->(Prints "b") #(Prints "a") <-> If Letter Matching Comes First then Number Matching # OR #(Prints "a") <-> Without g Modifier Based on FAQ 6.19, I'm trying to understand why Precedence matters when using the g Modifier when matching part of a string, even when that string hasn't been modified. I don't have any problems when using the g Modifier in a Loop, for some reason. From FAQ 6.19:[ref] > The notation "\G" is used in a match or substitution in conjunction > with > the "/g" modifier to anchor the regular expression to the point > just > past where the last match occurred, i.e. the pos() point.[/ref] Thanks in advance. -gohaku [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => <2B3C35D6-8689-11D8-AE5B-000A9574CFD8@earthlink.net> [ref] => [htmlstate] => on_nl2br [postusername] => Gohaku [ip] => gohaku@earthlin [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 1 [islastshown] => [isfirstshown] => 1 [attachments] => [allattachments] => ) --> Explanation of G Modifier - PERL Beginners
Professional Web Applications Themes

Explanation of G Modifier - PERL Beginners

Hi everyone, Sorry for asking this question again for what must be the umpteenth time but I have a problem with the g modifier when matching. I have read Newsgroup Postings about this topic including: "What good is \G in a regular expression?" Listed below is what I'm experiencing problems with: $P{"letter"} = "([A-Z])"; $P{"number"} = '(\d{1,2})'; $src = "a 1 b 2 c 3"; if($src =~ m/$P{"number"}/sgi) { $num = $1; } if($src =~ m/$P{"letter"}/sgi) { $let = $1; } print "Number: $num\n"; #-->(Prints "1") print "Letter: $let\n";#-->(Prints "b") #(Prints "a") <-> If Letter Matching Comes First then Number ...

  1. #1

    Default Explanation of G Modifier

    Hi everyone,
    Sorry for asking this question again for what must be the umpteenth
    time but I have a problem with
    the g modifier when matching. I have read Newsgroup Postings about
    this topic including:
    "What good is \G in a regular expression?"

    Listed below is what I'm experiencing problems with:

    $P{"letter"} = "([A-Z])";
    $P{"number"} = '(\d{1,2})';

    $src = "a 1 b 2 c 3";

    if($src =~ m/$P{"number"}/sgi)
    {
    $num = $1;
    }

    if($src =~ m/$P{"letter"}/sgi)
    {
    $let = $1;
    }

    print "Number: $num\n"; #-->(Prints "1")
    print "Letter: $let\n";#-->(Prints "b")
    #(Prints "a") <-> If Letter Matching Comes First then Number Matching
    # OR
    #(Prints "a") <-> Without g Modifier

    Based on FAQ 6.19, I'm trying to understand why Precedence matters when
    using the g Modifier when matching
    part of a string, even when that string hasn't been modified.
    I don't have any problems when using the g Modifier in a Loop, for some
    reason.

    From FAQ 6.19: 


    Thanks in advance.
    -gohaku

    Gohaku Guest

  2. #2

    Default Re: Explanation of G Modifier

    gohaku wrote: 


    G is likely best for s/// syntax as there is a
    better (?:test) for pattern matching in
    general:

    $_ = "I match it All, so you dont have to";

    s%a%_%ig;

    print "$_";

    you get
    I m_tch it _ll, so you dont h_ve to

    G just causes the match to match everywhere
    the pattern is to be found. But NOT beyond
    the end of the line (\n) which is a logical
    record separator...

    HTH;
    -Sx-
    Wc Guest

  3. #3

    Default Re: Explanation of G Modifier

    gohaku wrote:
     

    $_ = "a 1 b 2 c 3";

    print "I see numbers" if /(?!\d)/;

    __END__


    Or /(?!\w)/ et al...

    Maybe /pattern/sgi isn't what you want for your test?

    -Sx-
    Wc Guest

  4. #4

    Default Re: Explanation of G Modifier

    WC -Sx- Jones wrote: 
    >
    >
    >
    > G is likely best for s/// syntax as there is a
    > better (?:test) for pattern matching in
    > general:
    >
    > $_ = "I match it All, so you dont have to";
    >
    > s%a%_%ig;[/ref]

    Please, please don't do that. Using weirc chars obfuscates the code.
    Always uses slashes unless the regex contains multiple slashes or
    backslashes. When you do have to use a different regex quote character,
    please pick one that is not so noisy.
     

    The \G modifier causes the match to begin at the position of the last
    match. Ex.

    while ('a 1 b 2 c 3' =~ /\G\D+(\d)/g) { print $1 }

    => 123

    1st iteration:
    'a 1 b 2 c 3'
    p ^
    2nd iteration
    'a 1 b 2 c 3'
    p ^
    3rd iteration
    'a 1 b 2 c 3'
    p ^

    Where p marks the pos where the search begins, which is the position of
    the last match. And ^ marks the captured match.

    See 'perldoc perlre'

    Randy.
    Randy Guest

  5. #5

    Default Re: Explanation of G Modifier

    WC -Sx- Jones wrote: 
    >
    >
    >
    > G is likely best for s/// syntax as there is a
    > better (?:test) for pattern matching in
    > general:
    >
    > $_ = "I match it All, so you dont have to";
    >
    > s%a%_%ig;[/ref]

    Please, please don't do that. Using weirc chars obfuscates the code.
    Always uses slashes unless the regex contains multiple slashes or
    backslashes. When you do have to use a different regex quote character,
    please pick one that is not so noisy.
     

    The \G modifier causes the match to begin at the position of the last
    match. Ex.

    while ('a 1 b 2 c 3' =~ /\G\D+(\d)/g) { print $1 }

    => 123

    1st iteration:
    'a 1 b 2 c 3'
    p ^
    2nd iteration
    'a 1 b 2 c 3'
    p ^
    3rd iteration
    'a 1 b 2 c 3'
    p ^

    Where p marks the pos where the search begins, which is the position of
    the last match. And ^ marks the captured match.

    See 'perldoc perlre'

    Randy.

    Randy Guest

  6. #6

    Default Re: Explanation of G Modifier

    >> gohaku wrote: [/ref][/ref]

    while (<>) {
    chomp;
    PR: {
    m/ \G( \d+\b )/gcx && do { print "number: $1\n"; redo; };
    m/ \G( \w+ )/gcx && do { print "word: $1\n"; redo; };
    m/ \G( \s+ )/gcx && do { print "space: $1\n"; redo; };
    m/ \G( [^\w\d]+ )/gcx && do { print "other: $1\n"; redo; };
    }
    }

    Perl6 Faq ?
    -Sx-
    Wc Guest

Similar Threads

  1. Waitfor.exe explanation
    By Christophe in forum Windows Server
    Replies: 0
    Last Post: July 6th, 09:44 AM
  2. reasonable explanation
    By Carnivegan in forum Adobe Indesign Windows
    Replies: 1
    Last Post: June 5th, 05:15 PM
  3. [DIR MX] set pref explanation ?
    By greenshot in forum Macromedia Director Basics
    Replies: 1
    Last Post: October 8th, 01:49 AM
  4. Need Explanation
    By Kaladhaur Palaniappa in forum Informix
    Replies: 5
    Last Post: August 12th, 12:10 AM
  5. Explanation
    By SRam in forum Informix
    Replies: 0
    Last Post: August 9th, 08:00 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139