Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

searching backwards in a string

by visnu (Sexton)
on Jun 13, 2000 at 03:32 UTC ( #17811=perlquestion: print w/replies, xml ) Need Help??

visnu has asked for the wisdom of the Perl Monks concerning the following question:

is there a way to perform a regular expression search moving backwards through a string? eg, the last whitespace character?

maybe i'm asking the wrong question. i'm working on code that wraps text at say 80 columns.. the text can break at spaces and punctuation, but i have to do something like:

$i = rindex $_, ' ', $WRAP_WIDTH;
$next_line = substr $_, $i, length, "";

Replies are listed 'Best First'.
Re: searching backwards in a string
by plaid (Chaplain) on Jun 13, 2000 at 03:36 UTC
    You might want to take a look at one of the CPAN modules to do text wrapping, namely Text::Wrapper, Text::Wrap, or Text::Format.

    For a regular expression way to do it, how about:

    while($text =~ /(.{0,80})\s+/g) { # result in $1 }
      >> while($text =~ /(.{0,80})\s+/g) { # result in $1 } << I believe that the above snippet is an infinite loop. However, if TEXT is a filehandle then while(<TEXT> =~ /(.{0,80})\s+/g){ # result in $1 } should kill all trailing space and line returns. Isn't that what chomp is for, though?

        If all your code did was that very small snippet then it would indeed be an infinite loop. However, the loop was given as an example and the comment was supposed to be replaced with code that does something useful

        the inside of the loop should look something like: (untested)

        push @lines, substr $_, 0, length $1, "";

        Nuance

        The code snippit does not loop infinitely, regardless of loop body. As stated, the result is in $1. It then becomes trivial to write a test program to see how it works. In fact, I'll take care of it for you.
        use strict; my $text = "This is a long line that I am typing to demonstrate". " the way that my regular expression works and it's a". " run-on sentence too but I don't really care."; print $1, "\n" while($text =~ /(.{0,20})\s+/g);
        Has a slight problem of not grabbing the last line, but it's 99% of the way there, and besides, that's why you should use a module in the first place:)
      >> while($text =~ /(.{0,80})\s+/g) { # result in $1 } << I believe that the above snippet is an infinite loop. However, if TEXT is a filehandle then while(<TEXT> =~ /(.{0,80})\s+/g){ # result in $1 } should kill all trailing space and line returns. Isn't that what chomp is for, though?
Re: searching backwards in a string
by chromatic (Archbishop) on Jun 13, 2000 at 03:47 UTC
    You can take advantage of greediness, in this case: $string =~ /(.*)\s(\S*?)/; That'll match greedily before the white space and minimally after it. I agree with plaid, though. Use a CPAN module for wrapping.
RE: searching backwards in a string
by redmist (Deacon) on Jun 13, 2000 at 13:24 UTC
    You can always read over the Gory Details of the perl regexp engine in Programming Perl.

    redmist
Re: searching backwards in a string
by wayne keenan (Novice) on Jun 13, 2000 at 16:57 UTC
    anchor the search using the $ meta caracter (which
    represents end of string)
    
    fruitless extraction of trailing whitespace:
    
     /(\s+)$/
    
    $num_trailing_spaces=length $1;
    
    highly recommend oreilly book: Master REgular Expressions
    
    
Re: searching backwards in a string
by Kevman (Pilgrim) on Jun 13, 2000 at 23:47 UTC
    Being new to perl, please dont flame me.
    But, why cant you store the string in reverse in another string, and then work forwards...
    Eg.
    my($str) = "abcdefgh"; my($str_rev); print "before $str\n"; $str_rev = reverse($str); print "after $str_rev \n";
    This sets str_rev to "hgfedcba".
      i was thinking about that, but that's just horrid when you think about what's going on underneath compared to what the ideal case would be...
        For a "new" user of perl (me), its an adequate way round the problem until a person gains more knowledge.
        You shouldn't dismiss this out of hand.

        Kevman

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://17811]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2020-07-14 10:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?