Based on my first look at it I can already give these hints: (I'm still looking at it though)

(This first point follows on reasonablekeith's point:) You use \W*? to match optional whitespace, but \W represents a non-word charachter, and \s represents whitespace... also do you really want to make it non-greedy? I would suggest replacing \W*? with \s*, and also I would not give it the comment optional whitespace... (but that's just me)

What I supsect is wrong is this part of the regex: \W*?([^,]*?)\W*?,
You are making the [^,] optional and non greedy... Maybe \W*? eats all the non-comma symbols? Something like: \s*([^,]+),\s* might be better. Verifying by capturing \W*? shows that this is the problem. (as in $2 holds '")')

And, if you post a message with a regex then you might want to give both the input and the output you expect, you described it, but giving the actual string makes it easier for everyone to verify...

Update: I did some further investigating, and here is a regex that works... (or atleast as far as I can tell):

s/^ \s* my \s* \(?\s* \$page # page is the variable \s*\)? \s* = \s* \&? # function may or may not be explicitly contexted create_page # function name \s* \(\s* # Begin param ([^,]+?), # 1st param \s* ([^,]+?), # 2nd param \s* ([^,]+?), # 3th param \s* ([^,]+?) # 4th param \s*\) # End param ; /your_replace_string/x;

Also note that the /g in your regex is useless, since you have the ^... which will only match at the start of the string (unless ofcourse when you have /m too, but that's not the case in your example)


In reply to Re: replacing code with regex by Animator
in thread replacing code with regex by raflach

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.