John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:
Consider as a concrete example the ability to match list items in Wiki-style markup. Here is what I ended up using:
It's especially complicated for the fact the last thing could be missing completely. The interesting part here is the two uses of \S. The first makes sure the thing ends with a non-space. The second is a look-back assertion so the final trailing whitespace won't just be eaten by the main item (which can certainly include internal spaces). I considered doing something with a "cut" first, but didn't work it out.my ($prefix, $content) = $line =~ /^\s*([*#]+:?)\s*(.*\S)?(?:(?<=\S)\ +s*)?$/;
Is there some better (or clearer?) trick to doing this? Perhaps different ideas using newer regex features?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex: not the trailing whitespace
by ikegami (Patriarch) on Apr 23, 2011 at 05:36 UTC | |
by John M. Dlugosz (Monsignor) on Apr 23, 2011 at 06:48 UTC | |
by ikegami (Patriarch) on Apr 23, 2011 at 06:54 UTC | |
by John M. Dlugosz (Monsignor) on Apr 23, 2011 at 07:56 UTC | |
by ikegami (Patriarch) on Apr 23, 2011 at 08:01 UTC | |
| |
|
Re: Regex: not the trailing whitespace
by wind (Priest) on Apr 23, 2011 at 06:40 UTC | |
by John M. Dlugosz (Monsignor) on Apr 23, 2011 at 06:51 UTC |