l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
November 4: Social gathering
Next Installfest:
TBD
Latest News:
Oct. 10: LUGOD Installfests coming again soon
Page last updated:
2001 Dec 30 17:10

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] Looking for 2 cool regexs
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] Looking for 2 cool regexs



# foreach $chunk (m/\G(.{1,$length})\s+/gso) {     # get chunks

\G == begin where the last \g left off.
(.{1,$length}) == capture between 1 and X characters.
\s+ == get one or more whitespace characters
/gso == /g -- do this as many times as it works
        /s -- pretend the string is all one line (allow '.' to match \n)
        /o -- compile the regex once, so that things run much faster.

... note the thing that replaces newlines with space doesn't need to be
run... so the /s is in the provided re... striping newlines was just so
that printout looks easier to read.

    TTFN,
      Mike


msimons@fizban:~> cat test.pl 
#! /usr/bin/perl -w

$length = 40;                          # length of data
$_ = `fortune`;                        # get some random string

print $_, "=" x $length, "\n";         # print for debugging
s/\n/ /g;                              # strip newlines to make printing nicer
foreach $chunk (m/\G(.{1,$length})\s+/gso) {     # get chunks
  printf "%3d: %2d, \"%s\"\n", $i++, length($chunk), $chunk;
}

exit 0;
====


msimons@fizban:~> ./test.pl 
The health of a democratic society may be measured by the quality
of functions performed by private citizens.
                -- Alexis de Tocqueville
========================================
  0: 38, "The health of a democratic society may"
  1: 39, "be measured by the quality of functions"
  2: 35, "performed by private citizens.                --"
  3: 21, "Alexis de Tocqueville"
====


msimons@fizban:~> ./test.pl 
Everybody knows that the dice are loaded.  Everybody rolls with their
fingers crossed.  Everybody knows the war is over.  Everybody knows the
good guys lost.  Everybody knows the fight was fixed: the poor stay
poor, the rich get rich.  That's how it goes.  Everybody knows.

Everybody knows that the boat is leaking.  Everybody knows the captain
lied.  Everybody got this broken feeling like their father or their dog
just died.

Everybody talking to their pockets.  Everybody wants a box of chocolates
and long stem rose.  Everybody knows.

Everybody knows that you love me, baby.  Everybody knows that you really
do.  Everybody knows that you've been faithful, give or take a night or
two.  Everybody knows you've been discreet, but there were so many people
you just had to meet without your clothes.  And everybody knows.

And everybody knows it's now or never.  Everybody knows that it's me or you.
And everybody knows that you live forever when you've done a line or two.
Everybody knows the deal is rotten: Old Black Joe's still pickin' cotton
for you ribbons and bows.  And everybody knows.
        -- Leonard Cohen, "Everybody Knows"
========================================
  0: 33, "Everybody knows that the dice are"
  1: 35, "loaded.  Everybody rolls with their"
  2: 37, "fingers crossed.  Everybody knows the"
  3: 38, "war is over.  Everybody knows the good"
  4: 37, "guys lost.  Everybody knows the fight"
  5: 39, "was fixed: the poor stay poor, the rich"
  6: 31, "get rich.  That's how it goes. "
  7: 38, "Everybody knows.  Everybody knows that"
  8: 37, "the boat is leaking.  Everybody knows"
  9: 37, "the captain lied.  Everybody got this"
 10: 35, "broken feeling like their father or"
 11: 39, "their dog just died.  Everybody talking"
 12: 40, "to their pockets.  Everybody wants a box"
 13: 34, "of chocolates and long stem rose. "
 14: 38, "Everybody knows.  Everybody knows that"
 15: 40, "you love me, baby.  Everybody knows that"
 16: 36, "you really do.  Everybody knows that"
 17: 36, "you've been faithful, give or take a"
 18: 37, "night or two.  Everybody knows you've"
 19: 37, "been discreet, but there were so many"
 20: 40, "people you just had to meet without your"
 21: 35, "clothes.  And everybody knows.  And"
 22: 35, "everybody knows it's now or never. "
 23: 40, "Everybody knows that it's me or you. And"
 24: 37, "everybody knows that you live forever"
 25: 31, "when you've done a line or two."
 26: 39, "Everybody knows the deal is rotten: Old"
 27: 40, "Black Joe's still pickin' cotton for you"
 28: 40, "ribbons and bows.  And everybody knows. "
 29: 35, "-- Leonard Cohen, "Everybody Knows""
====

On Fri, Sep 14, 2001 at 09:57:42AM -0500, Jay Strauss wrote:
> Ok, I've got another:
> 
> I have a really long string (2000ish chars), I want to break it into
> multiple strings of 80 chars or less.
> The Catch is, I need to break it on a space (' '), hence the "or less".  It
> seems, I should be able to
> use a regex on this.
> 
> Currently I do it like:
> 
> $lineLength = 80;
> $line = #some really long string#;
> 
> # essentially while the line is longer than 80 chars
> # find the right most ' ' or ',' up to a maximum of 80 chars
> # from the beginning
> # break off that piece of the string and start over
> while (length($line) > $lineLength) {
>    my $space = rindex(substr($line,0,$lineLength),' ') ||
>       rindex(substr($line,0,$lineLength),',');
>       print substr($line,0,$space)."\n";
>       substr($line,0,$space) = '';
> }
> print "$line\;\n";
> 
> Seems like I should be able to do it (somehow) with a regex sorta like
> 
> foreach ($line =~ m/(" ")/g) { # but somehow specify a length
>     print $_;
> }
> 
> Any ideas?
> Thanks
> Jay
> 
> 
> ----- Original Message -----
> From: "Ted Deppner" <ted@psyber.com>
> To: <vox-tech@franz.mother.com>
> Sent: Friday, September 14, 2001 3:36 AM
> Subject: Re: [vox-tech] Looking for 2 cool regexs
> 
> 
> > On Thu, Sep 13, 2001 at 11:05:48AM -0500, Jay Strauss wrote:
> > > find any CREATEwhitespaceTABLEanycharsnot; and replace them with ''.
> the
> > > whole lot of it has to start at a word boundary.
> >
> > right on...
> >
> > > I've never done this before, I didn't know you could produce a  list
> from a
> > > match command.
> >
> > It's nifty...  the concept is an expansion on
> >   ($a,$b,$c) = $var =~ m/(re1)(re2)(re3)/
> >   (where $1,$2,$3 are returned in a list context)
> >
> > --
> > Ted Deppner
> > http://www.psyber.com/~ted/
> 
> 
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com


LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
EDGE Tech Corp.
For donating some give-aways for our meetings.