G'day demichi,
Welcome to the monastery.
First, some issues with your post:
-
Second field:
-
Sample data shows no spaces: POLS and POLN (twice)
-
Attempted regex shows no matching of spaces for this field: /^(\w+)\s+(\w+)\s+.../
-
Description says: "1-n characters with 0-n spaces"
I've assumed no spaces.
-
Third record, fourth field:
-
Sample data shows: DemoSolutions
-
Actual and expected output show: Demo
I've assumed a typo.
-
Terminal spaces in sample data records:
-
First record has none
-
Second and third records have one
-
Attempted regex (/...\s+/) seems to indicate one or more (although, there's no end-of-line assertion)
I've allowed for zero or more.
-
Actual and expected output show a terminal semicolon for each record but this doesn't equate with a field separator.
I've used ($) to generate a final, zero-length field; you may want to change this.
Taking those assumptions (and other points) into account, this regex achieves what I think you want:
#!/usr/bin/env perl -l
use strict;
use warnings;
my $re = qr{
^ # start of l
+ine
(\S+)\s+(\S+)\s+(\S+)\s+ # 3 fields,
+no spaces
(.+?)\s+ # 1 field, +
+/- spaces
(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+) # 7 fields,
+no spaces
\s* # possible s
+paces
($) # end of lin
+e
}x;
print join ';' => /$re/ while <DATA>;
__DATA__
PID POLS U(%) POOL_NAME Seq# Num LDEV# H(%) VCAP(%) TYPE PM
003 POLN 0 Bad name with spaces 13453 2 61443 80 - OPEN N
002 POLN 52 DemoSolutions 54068 7 61454 80 - OPEN N
Output:
PID;POLS;U(%);POOL_NAME;Seq#;Num;LDEV#;H(%);VCAP(%);TYPE;PM;
003;POLN;0;Bad name with spaces;13453;2;61443;80;-;OPEN;N;
002;POLN;52;DemoSolutions;54068;7;61454;80;-;OPEN;N;
-
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.
|