Can I know the position of the end of an index() match? 
Author Message
 Can I know the position of the end of an index() match?

When I use index() on a string and a regexp I get the offset of
the match. Is there a way I can get te length of the match too?

I would know something to do:

        ($first, $match, $rest) = $string =~ /(.*)($pattern)(.*)/;

then $match would give me the length of the pattern match and index() could
give me the position.

        $pos = index( $string, $match);

But that means two matches and I would like to do only one.

--
Gerben Wierda
Phone: (+31) 2154 84415                 Home: (+31) 85 516677
        "If you don't know where you are going, any road will take you there."
        Lewis Carroll, "Alice in Wonderland".



Mon, 28 Mar 1994 21:03:21 GMT  
 Can I know the position of the end of an index() match?
How about:

$pos = length((($first, $match, $rest) = $string =~ /(.*)($pattern)(.*)/)[0]);

|> When I use index() on a string and a regexp I get the offset of
|> the match. Is there a way I can get te length of the match too?
|>
|> I would know something to do:
|>
|>   ($first, $match, $rest) = $string =~ /(.*)($pattern)(.*)/;
|>
|> then $match would give me the length of the pattern match and index() could
|> give me the position.
|>
|>   $pos = index( $string, $match);
|>
|> But that means two matches and I would like to do only one.
|>
|> --
|> Gerben Wierda
|> Phone: (+31) 2154 84415                   Home: (+31) 85 516677
|>   "If you don't know where you are going, any road will take you there."
|>   Lewis Carroll, "Alice in Wonderland".



Tue, 29 Mar 1994 21:12:24 GMT  
 Can I know the position of the end of an index() match?
|> When I use index() on a string and a regexp I get the offset of
|> the match. Is there a way I can get te length of the match too?
|>
|> I would know something to do:
|>
|>   ($first, $match, $rest) = $string =~ /(.*)($pattern)(.*)/;

Since Randal gave me such a polite pointer to the manual (thank you,
Randal), I guess I should do the same.

    $& The string matched by the last pattern match

    $` The string preceding whatever was matched by the last pattern match

    $' The string following whatever was matched by the last pattern match
     Example:
              $_ = 'abcdefghi';
              /def/;
              print "$`:$&:$'\n";     # prints abc:def:ghi

Therefore:

    $string =~ /$pattern/;
    ($first, $match, $rest) = ($`,$&,$');
    $len = length($match);
    $pos = length($first)+1;

This could be reduced to:
     $string =~ /$pattern/;
     $len = length($&);
     $pos = length($`)+1;

In either case, single match, no parentheses in the reg exp, and less
explicit backtracking.  /(.*).../ is not a good idea if the match
might fail, because it will then re-try to match at the second char,
third char, ... .

Re: multi-line split
Thanks again Randal for the pointer to the manual entry.  

This is an example of why it is dangerous to load two purposes on one
syntactic construct.  I needed a way to group an expression to make it
the object of the + operator, and got a side-effect.  PERL really
needs a grouping symbol other than () for cases like this.

My solution to the problem was:
    $* = 1;
    split(/^\..*\n/);

The problem was to fetch the data lines of an nroff document,
considering text between sequences of commands as single fields.  

Gary Levin/Bellcore PYA 1E132/Piscataway, NJ 08854

--
Gary Levin/Bellcore PYA 1E132/Piscataway, NJ 08854



Wed, 30 Mar 1994 01:45:29 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. $ matches end-of-line *or* end-of-string!

2. How can i delete an array element i don't know the position

3. Don't know how to reference array position from a foreach loop

4. match from the end or match the last

5. I'll bet Martien knows (transp index color)

6. how to get the index of an array knowing the value

7. substitution/pattern matching -- setting the position...

8. Get the matched position in regexp

9. Getting positions in matched string

10. Getting a position of match in regex

11. How to determine position of a match?

 

 
Powered by phpBB® Forum Software