Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
So on OpenProjectsNet IRC today, someone asked how to scrunch chunks of whitespace into a single space, unless the chunk was "\n\n" (in which case it doesn't get altered). So I gave him:
s/(\s+)/$1 eq "\n\n" ? $1 : " "/eg;
That was fine. But I wondered, how should "\t\n\n" or "\n\n\t" be handled? Well, I came up with a truly hideous, yet truly working, regex. One regex. With no /e modifier. One catch: variable-width look-behinds. How did I get around it? Why, sexeger of course. This was another use of that technique to solve an interesting problem. You see, "\n\n\n" should match the "\n\n" as one unit (leaving it intact) but then "\n" as a chunk to be turned into a single space. However, "\n\n\n\n" should be seen as two "\n\n" units.

The problem is that when I come across a newline, I need to see if it is preceded by an even number of newlines. Ordinarily, I'd say /\n(?=(?:\n\n)*(?!\n))/ to denote "a newline followed by an even number of newlines". Sadly, I can't use that for look-behind: /(?<=(?<!\n)(?:\n\n)*)\n/ doesn't work because it's variable width. Solution? Reverse it.

So I offer this regex:

($_ = reverse) =~ s{ (?: [\r\t\f ]+ # non-\n whitespace | # OR (?<!\n) # not preceded by a \n \n # match a \n (?= # that's followed by... (?:\n\n)* (?!\n) # an even number of \n's ) )+ # one or more times }{ }xg; # turn it into a single space $_ = reverse;
Whew.

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;


In reply to (Regex Madness) And you thought whitespace was easy. by japhy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-29 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found