Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Regexpression is not matching

by tobias_hofer (Friar)
on Jun 05, 2013 at 14:15 UTC ( #1037230=perlquestion: print w/replies, xml ) Need Help??
tobias_hofer has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

I am developing a tool for analyzing different map-files. I do this by reading-in the map-file generated by i.e. ARM compiler into an Array ( as usual ) and start analyzing line by line by going through the array.

A small state machine shall do the analysis. In case specific content is found the state machine jumps into different state. One of this state-changes shall be done when following line is recognised
"      Code (inc. data)   RO Data    RW Data    ZI Data      Debug   Object Name\n".

Now I want to have a regex which matches this and only this line, so I started doing this like that:
 if ( $MapFile->[$iterator] =~m#      Code \(inc\. data\)   RO Data    RW Data    ZI Data      Debug   Object Name#){ something... [...]

I have escaped the braces and the dot but it does not match. ?? I want to keep it also quite simple so i have not used \s+ in the regex, but even with \s+ it does not match. I do not want to get any information from the expression so i am not using braces at all, just the match is quite important to me.

Any help is welcome!!

Best regards!

Replies are listed 'Best First'.
Re: Regexpression is not matching
by BrowserUk (Pope) on Jun 05, 2013 at 14:22 UTC

    If you want to match that exactly line and nothing else and extract no date, don't use a regex. Use eq:

    use constant THE_LINE => q[ Code (inc. data) RO Data RW Data + ZI Data Debug Object Name]; ... if( $MapFile->[ $iterator ] eq THE_LINE ) { ... }

    As for why your regex doesn't match.

    Did you chomp the newlines off when you read the lines in? Does the leading whitespace contain tabs perhaps? Or elsewhere in the line?

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks a lot! Yes, i have forgot to chomp the newline off and I will use equal instead of regex!

      Best regards!
Re: Regexpression is not matching
by ww (Archbishop) on Jun 05, 2013 at 14:31 UTC

    This works for me, making me wonder if there's a typo or variance between spaces and tabs, or somesuch in your problem statement or if the dot at the end of your sample data line is somehow included in your code:

    #!/usr/bin/perl use 5.016; # 1037230 my $string = " Code (inc. data) RO Data RW Data ZI Data + Debug Object Name\n"; # no trailing full stop/dot/period if ( $string =~m# Code \(inc\. data\) RO Data RW Data ZI +Data Debug Object Name#) { say "Good to go"; }
    That which looks like a space above, is.

    If you didn't program your executable by toggling in binary, it wasn't really programming!

      I just have had a look, it was a missing space between ZI Data and Debug. Guess i need new glasses ;-)

      Best regards! Tobias
      For me too....
      perl -e 'my $val=" Code (inc. data) RO Data RW Data ZI Da +ta Debug Object Name\n"; print "Yes" if $val =~ m# Code \ +(inc\. data\) RO Data RW Data ZI Data Debug Object Nam +e#;'


      Might be some space or chomp issue I belive

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1037230]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2018-06-21 18:40 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.