multiline, multi pattern match 
Author Message
 multiline, multi pattern match

 I'm doing pattern matching on a long ( 100K ) string ( contains
 no newlines ) like this:

 s/pattern1(.*)pattern2/$1/ig;

 My problem is that during multiple matches, the variable $1
 can only hold one single occurence.

 How can I save  a l l  the matches ?

  hans

=========================================================================

LindMac A/S                                         Phone: +4773 96 73 11
7038 Trondheim,  NORWAY                             Fax:    . . . . 54 79
=========================================================================



Sun, 16 Jun 1996 22:29:48 GMT  
 multiline, multi pattern match

Quote:

> I'm doing pattern matching on a long ( 100K ) string ( contains
> no newlines ) like this:

> s/pattern1(.*)pattern2/$1/ig;

> My problem is that during multiple matches, the variable $1
> can only hold one single occurence.

> How can I save  a l l  the matches ?


If you are expecting multiple matches then watch out for the .* which is
greedy and can quite happily eat up instances of pattern1 and pattern2
between the first pattern1 and last pattern2 in the string.  If you are
just looking for matches as opposed to doing substitutions then you can do
something like (in this case using { and } to delimit the interesting bits,
which makes it easy to avoid the right hand }s ;-):

#!/usr/bin/perl

$_ = '{part 1} blah blah {part 2} blah blah {part 3} foo';



If your pattern1 and pattern2 are more complex than simple characters
then this approach is not too useful...

Hope this helps (a bit)

Mike

--
The "usual disclaimers" apply.    | Meiko
Mike Stok                         | Reservoir Place

Meiko tel: (617) 890 7676         | Waltham, MA 02154



Mon, 17 Jun 1996 19:42:48 GMT  
 multiline, multi pattern match

Quote:

>  I'm doing pattern matching on a long ( 100K ) string ( contains
>  no newlines ) like this:

>  s/pattern1(.*)pattern2/$1/ig;

>  My problem is that during multiple matches, the variable $1
>  can only hold one single occurence.

>  How can I save  a l l  the matches ?

If the input doesn't contain newlines, your pattern will always match
at most once, since '(.*)' will gobble everything from the _first_
pattern1 to the _last_ pattern2.  The 'g' modifier doesn't do anything.

What do you want to accomplish?  Could you explain a bit more of the
context, or perhaps give an example? (What input should be transformed
into what output?)

--
Uwe Waldmann, Max-Planck-Institut fuer Informatik
Im Stadtwald, D-66123 Saarbruecken, Germany



Mon, 17 Jun 1996 21:20:27 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Pattern bug matching whitespace in multi-line match?

2. bug in anchored, multiline pattern match

3. pattern matching and multilines (sort of)

4. Multiline pattern matching with command line invocation

5. regex to match a multi line pattern

6. Multi line pattern matches?

7. Multi-line pattern matching?

8. pattern matching in multi-line strings fails under perl4.034

9. regex to match a multi line pattern

10. Multi-line pattern matching

11. Multi line pattern match

12. Multi-Inclusive Pattern Match

 

 
Powered by phpBB® Forum Software