harangzsolt33 has asked for the wisdom of the Perl Monks concerning the following question:
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Multiline regex
by kcott (Archbishop) on Dec 18, 2022 at 04:04 UTC | |
G'day harangzsolt33, "Is it possible to "break up" a regex so that it spans multiple lines?" The /x and /xx modifiers exist for this purpose. See "perlre: Modifiers: /x and /xx". There are quite a few gotchas associated with these. In http://wzsn.net/perl/index.html, you say you're using "TinyPerl 5.8". I'm not familiar with this, but I'll assume it's a cut-down version of the standard "Perl 5.8"; I don't know what features or support it modifies or excludes. The following version notes refer to "Perl 5.8"; you'll need to adjust for any "TinyPerl 5.8" limitations. (Perl v5.8.0 was released over 20 years agoperlhist; you're missing out on many features, bug & security fixes, and optimisations, with such an old version; an upgrade is recommended.) I personally find the /x modifier to be very helpful, particularly with respect to improved readability, and use it often (except for the simplest regexes). On the other hand, I'm not convinced that /xx offers equivalent enhancements; making changes can, on occasion, be tricky. Of course, those are my preferences; they're not recommendations, make your own choices. When using either /x or /xx, you need to be mindful of whitespace and hash characters. Here's a non-exhaustive demonstation of some of the similarities and differences:
If I uncomment line 16 (s/[ ]//gxx), I get a single line of output:
Line 17, with s/[\ ]//gxx, fixes this. It also demonstrates one of the traps for the unwary. As well as adding modifiers to the end of m// and s///, you can also embed them in "Extended Patterns". I find this is handy when used with qr//:
Examples of usage crop up here fairly often. A couple of my most recent offerings: "a fairly simple example"; "a more involved, and fully commented, example". And, from many years ago, "a very long and complex example, using qr{...}msx". — Ken | [reply] [d/l] [select] |
by harangzsolt33 (Deacon) on Dec 18, 2022 at 20:01 UTC | |
| [reply] |
by Fletch (Bishop) on Dec 19, 2022 at 08:57 UTC | |
Rather than reimplementing something to try and (most likely, poorly and/or incompletely) parse Perl let it do it for you with B::Xref.
The cake is a lie. | [reply] |
Re: Multiline regex
by johngg (Canon) on Dec 17, 2022 at 22:11 UTC | |
Yes. See the x modifier in perlre. Cheers, JohnGG | [reply] [d/l] |