location of regular expression match

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default location of regular expression match

    I apologize for another newbie question. How does one obtain the
    starting character position of all regular expression matches in a
    line or a paragraph?
    Thank you.
    bob Guest

  2. Similar Questions and Discussions

    1. Regular expression help
      Hi, I'm pretty new to regular expressions. Before, I used to write long-winded and buggy segments of code with PHPs string functions to extract...
    2. Regular expression for both first and last name?
      I'm new to regular expressions, can someone explain to me how I can write one that will check for 2 names, at least, for a name field? Thanks!...
    3. Regular expression bug?
      All of CF's RE functions act in a weird way, contrary to the documentation (both CF's own, and the underlying Java Regex docs). The special...
    4. help on regular expression
      Hi, I need some help on regular expression... i have following in variable $total_count $total_count = "##I USBP 000001 10:38:09(000)...
    5. [PHP] REGULAR EXPRESSION HELP
      John wrote: Your "newline" may be \r\n or \r instead of just \n. -- ---John Holmes... Amazon Wishlist:...
  3. #2

    Default Re: location of regular expression match

    bob wrote:
    > I apologize for another newbie question. How does one obtain the
    > starting character position of all regular expression matches in a
    > line or a paragraph?
    One way I can think of is using pos() in a loop:


    foreach $token (qw#foo bar#) {
    $position= pos($line),"\n" if $line =~ /$token/g;
    # now do something with it
    }

    I _hope_ there is a more elegant WTDI.

    Amir

    Amir Kadic Guest

  4. #3

    Default Re: location of regular expression match

    I wrote:
    > $position= pos($line),"\n" if $line =~ /$token/g;
    Please forget the q(,"\n")

    This was originally a print(), but then I thought,
    'that man wants to _obtain_, not print the positions'...:)

    Amir

    Amir Kadic Guest

  5. #4

    Default Re: location of regular expression match

    <snip>
    > I apologize for another newbie question. How does one obtain the
    > starting character position of all regular expression matches in a
    > line or a paragraph?
    > Thank you.

    It is stored in the @- array (ie. $-[0] is the position at the start of a
    match). Use //g modifier and \G anchor as needed. Check out perlretut and
    perlre for more info.

    Moose
    mooseshoes Guest

Posting Permissions

  • You may not post new threads
  • You may 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