First the regex that does what you want (as most regexes assuming a lot): $line =~ s/\s+(\S+)\s+/:$1:/;
which will replace the first instance of "a series of whitespace followed by a series non-whitespace followed by a series of whitespace", with a "colon, followed by the set of non-whitespace matched, followed by another colon". You will have to modify this further to get the result you posted as it is also removing the set of spaces after "three" and replacing it with one space.
Any idea what I'm doing wrong?
Your code can be broken down to a pattern like so:
. will match any character besides \n. The {2} at the end means do this twice. This has to be followed by
\s+ which means the pattern has to have at least one whitespace character but will capture as many of these in a row as it can.
When your pattern matches the first time in the line it will replace it with a colon. As per usual,let me direct you to perlre for your regex needs. -enlil
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|