If you want to put the x inside the regex you can do it like this (ripping off Utilitarian's code):

my $regex =qr{(?x) ^(\w*)?\s #A comment (\w*)?\s # an other (\w*)?\s # and again (?:([A-Z]*)\s)? # all the way through (\d+)\* # so that the homicidal psychopath (?:([0-9+]+)[>])? # who knows where you live ([0-9+]+)\s. # and will have to maintain this code (\d*) # will think well of you };

Note that the (?x) must immediately follow the first regex delimiter otherwise the whitespace before will be a literal part of the pattern (this was not the case with earlier Perl versions, 5.6 IIRC) and note also that using qr{...} creates a compiled regex which you assign to a scalar for later use. The nice thing about the (?x) syntax is the ability to switch behaviours on and off for different regions in your pattern whereas the /.../x modifier applies to the whole pattern. This is probably more useful with something like case insensitivity and such use is illustrated in this reply in a thread started by Win.

I hope this is helpful.

Cheers,

JohnGG


In reply to Re^3: how to write a multi-line regex by johngg
in thread how to write a multi-line regex by zerocred

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.