Need help trying to do multi-line regexp 
Author Message
 Need help trying to do multi-line regexp

Hi.  I am having no luck at trying to match multi-line strings of the
format shown below.  

================================================================
 <VariableFormat
  <VariableName `cite-Smith90'>
  <VariableDef `46'>
 > # end of VariableFormat
 <VariableFormat
  <VariableName `cite-Smith93'>
  <VariableDef `64'>
 > # end of VariableFormat
================================================================

I am trying to use the following regexp, but it doesn't work. Why?
        $* =1;
        if(/(<VariableName\s*`cite\-\w*\'>\s*\n\s*<VariableDef\s*`[^']*'>)/){
                doSomethign...
         }
        $* =0;

Thanks

Curtis
--
-------------------------------------------------------------------------------
| Curtis Hrischuk (PhD Cand)  | Boundaries of Science:                        |

| Carleton University         | 2) Why am I here?                             |
| Ottawa, On. ,Canada, K1S-5B6| 3) What happens when I leave?                 |
| Ph  (613) 788-2600 x1762    | "The proof is almost identical to the previous|
| FAX (613) 788-5727          | proof and hence omitted here."(actual quote)  |



Sat, 27 Jun 1998 03:00:00 GMT  
 Need help trying to do multi-line regexp
 [courtesy cc of this posting sent to cited author via email]

In comp.lang.perl.misc,

:Hi.  I am having no luck at trying to match multi-line strings of the
:format shown below.  

: <VariableFormat
:  <VariableName `cite-Smith90'>
:  <VariableDef `46'>
: > # end of VariableFormat
: <VariableFormat
:  <VariableName `cite-Smith93'>
:  <VariableDef `64'>
: > # end of VariableFormat

:I am trying to use the following regexp, but it doesn't work. Why?

:   $* =1;
:   if(/(<VariableName\s*`cite\-\w*\'>\s*\n\s*<VariableDef\s*`[^']*'>)/){
:       doSomethign...
:   }
:   $* =0

Because you've fallen prey to the false assumption that being able
to regexp parse multiline strings with $* has anything whatsoever with
imputting multiline strings.  It doesn't -- or at least, setting $* has
absolulutely zilcho effect on the $/ variable, which is what you need.

Plus, you shouldn't be using $* -- it's deprecated.  Contrary to popular
misconception, the *ONLY* thing that that variable does is determine
whether or not ^ and $ in patterns should be able to match repeatedly
(that's what the /m flag is for) It does NOT read it more lines, nor does
it say whether a dot in a pattern should also match a newline (that's what
the /s flag is for).

Instead do something more like this, and add some comments: :-)

    #!/usr/bin/perl -w
    require 5.001;
    use strict;
    $/ = ''
    while ( <> ) {
        if ( m{ (
                 < VariableName \s*
                    ` cite - .*? '
                 >
                  \s*
                 < VariableDef  \s*
                    ` .*? '
                 >
            ) }xs )
        {
            # do something
        }

There's no reason to be writing \- or \`, and I'm not sure that
\s*\n\s* shouldn't just be \s*, and the funny [^>]* construct is better
written as .*?, but if you want to cross newline boundaries, you'll
need a /s, and the /x is very nice for formatting, and you can even
embed comments.

Have fun.

--tom
--

You have made an excellent hit on the UNIX.--More--



Sat, 27 Jun 1998 03:00:00 GMT  
 Need help trying to do multi-line regexp

Quote:

>   Hi.  I am having no luck at trying to match multi-line strings of the
>   format shown below.  

>   ================================================================
>    <VariableFormat
>     <VariableName `cite-Smith90'>
>     <VariableDef `46'>
>    > # end of VariableFormat
>    <VariableFormat
>     <VariableName `cite-Smith93'>
>     <VariableDef `64'>
>    > # end of VariableFormat
>   ================================================================

>   I am trying to use the following regexp, but it doesn't work. Why?
>       $* =1;
>       if(/(<VariableName\s*`cite\-\w*\'>\s*\n\s*<VariableDef\s*`[^']*'>)/){
>               doSomethign...
>        }
>       $* =0;

Works for me (Perl 5.001m).  Maybe your program isn't reading the text
correctly.  BTW, you don't need to set $* if you don't use ^ or $ in the
regexp (is my reading of the documentation).

-Tom



Wed, 01 Jul 1998 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. multi-line regexp help needed

2. Need help with multi-line regexp

3. help with multi-line regexp subsitution

4. Multi-line processing help needed

5. Newbie needs help- trying to submit command-line function to perl script via web

6. multi-line mode regexp

7. regexp %%CODE%% multi-line sub in template

8. Multi-line regexp searches? (Q)

9. Substitution with multi-line regexp - I give up!

10. Multi-line regex w/ multi-file loop

11. regexp needed for input line

12. help with multi-line pattern requested

 

 
Powered by phpBB® Forum Software