Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

It gets the second and third line just fine

Proof:

while(<DATA>){ print; if ($_ =~ /^\s*(\d+)\s+(.*?) (\s*)([WS|76])(.*?)\s+(\w+)\s+(.*)\s+( +.*)/) { print "SLOT = $1\n"; print "Desc = $2\n"; print "Model = $3\n"; print "\n"; } } close(FILE); __DATA__ Mod Sub-Module Model Serial ---- --------------------------- ------------------ ----------- 1 Distributed Forwarding Card WS-F6700-DFC3CXL SAL1434RLPY 3 7600 ES+ DFC XL 7600-ES+3CXL JAE14520N29 3 7600 ES+T 20x1GE SFP 76-ES+T-20GQ JAE145301XM 5 Policy Feature Card 3 7600-PFC3CXL JAE14330E6J 5 C7600 MSFC4 Daughterboard 7600-MSFC4 JAE14320QBE 6 Policy Feature Card 3 7600-PFC3CXL JAE14330EAO 6 C7600 MSFC4 Daughterboard 7600-MSFC4 JAE14320QA8 7 Distributed Forwarding Card WS-F6700-DFC3CXL SAL1433QHBR 8 Distributed Forwarding Card WS-F6700-DFC3CXL SAL1433QXF9
Mod Sub-Module Model Serial ---- --------------------------- ------------------ ----------- 1 Distributed Forwarding Card WS-F6700-DFC3CXL SAL1434RLPY 3 7600 ES+ DFC XL 7600-ES+3CXL JAE14520N29 SLOT = 3 Desc = Model = 3 7600 ES+T 20x1GE SFP 76-ES+T-20GQ JAE145301XM SLOT = 3 Desc = Model = 5 Policy Feature Card 3 7600-PFC3CXL JAE14330E6J 5 C7600 MSFC4 Daughterboard 7600-MSFC4 JAE14320QBE 6 Policy Feature Card 3 7600-PFC3CXL JAE14330EAO 6 C7600 MSFC4 Daughterboard 7600-MSFC4 JAE14320QA8 7 Distributed Forwarding Card WS-F6700-DFC3CXL SAL1433QHBR 8 Distributed Forwarding Card WS-F6700-DFC3CXL SAL1433QXF9

But in your regex I notice a few "funny" parts:

(\s*)

Are you sure you want to capture the whitespaces? I assume you don't.

([WS|76])(.*?)

Are you sure you mean [ and ] here? They signify character classes (much like how \d means [0-9], or "any one digit", [WS] means "any one W or S"), not capture groups. Assuming you meant (WS|76), then do you really want to capture the (WS or 76) separately from whatever follows? My guess is that you meant ((?:WS|76)).*?) here — and I doubt you actually need to specify that this substring begins with the WS or 76, so why not just simplify it to (\S+)?

\s+(.*)\s+(.*)

I'm not sure which parts of the sample data this would match. Did you really intend to have it there?

Modifying your regex according to the above assumptions, and tacking on a $ for good measure, I come up with /^\s*(\d+)\s+(.+?)\s*(\S+)\s+(\w+)$/. Furthermore, I threw in a chomp because I don't want to be bothered with the newlines that are tacked onto each value for $_, so eventually my version of your script looks like this:</c>

while(<DATA>){ print; chomp; if ($_ =~ /^\s*(\d+)\s+(.+?)\s*(\S+)\s+(\w+)$/) { print "SLOT = $1\n"; print "Desc = $2\n"; print "Model = $3\n"; print "\n"; } } close(FILE); __DATA__ Mod Sub-Module Model Serial ---- --------------------------- ------------------ ----------- 1 Distributed Forwarding Card WS-F6700-DFC3CXL SAL1434RLPY 3 7600 ES+ DFC XL 7600-ES+3CXL JAE14520N29 3 7600 ES+T 20x1GE SFP 76-ES+T-20GQ JAE145301XM 5 Policy Feature Card 3 7600-PFC3CXL JAE14330E6J 5 C7600 MSFC4 Daughterboard 7600-MSFC4 JAE14320QBE 6 Policy Feature Card 3 7600-PFC3CXL JAE14330EAO 6 C7600 MSFC4 Daughterboard 7600-MSFC4 JAE14320QA8 7 Distributed Forwarding Card WS-F6700-DFC3CXL SAL1433QHBR 8 Distributed Forwarding Card WS-F6700-DFC3CXL SAL1433QXF9
Mod Sub-Module Model Serial ---- --------------------------- ------------------ ----------- 1 Distributed Forwarding Card WS-F6700-DFC3CXL SAL1434RLPY SLOT = 1 Desc = Distributed Forwarding Card Model = WS-F6700-DFC3CXL 3 7600 ES+ DFC XL 7600-ES+3CXL JAE14520N29 SLOT = 3 Desc = 7600 ES+ DFC XL Model = 7600-ES+3CXL 3 7600 ES+T 20x1GE SFP 76-ES+T-20GQ JAE145301XM SLOT = 3 Desc = 7600 ES+T 20x1GE SFP Model = 76-ES+T-20GQ 5 Policy Feature Card 3 7600-PFC3CXL JAE14330E6J SLOT = 5 Desc = Policy Feature Card 3 Model = 7600-PFC3CXL 5 C7600 MSFC4 Daughterboard 7600-MSFC4 JAE14320QBE SLOT = 5 Desc = C7600 MSFC4 Daughterboard Model = 7600-MSFC4 6 Policy Feature Card 3 7600-PFC3CXL JAE14330EAO SLOT = 6 Desc = Policy Feature Card 3 Model = 7600-PFC3CXL 6 C7600 MSFC4 Daughterboard 7600-MSFC4 JAE14320QA8 SLOT = 6 Desc = C7600 MSFC4 Daughterboard Model = 7600-MSFC4 7 Distributed Forwarding Card WS-F6700-DFC3CXL SAL1433QHBR SLOT = 7 Desc = Distributed Forwarding Card Model = WS-F6700-DFC3CXL 8 Distributed Forwarding Card WS-F6700-DFC3CXL SAL1433QXF9 SLOT = 8 Desc = Distributed Forwarding Card Model = WS-F6700-DFC3CXL

Which sure looks good to me.


In reply to Re: How we can do regex of this? by muba
in thread How we can do regex of this? by xMiDgArDx

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 imbibing at the Monastery: (7)
As of 2024-03-28 19:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found