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:
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: Whew.
_____________________________________________________ In reply to (Regex Madness) And you thought whitespace was easy. by japhy
|
|