Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re: simple regex problem by Enlil
in thread simple regex problem by Anonymous Monk

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



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-26 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found