http://www.perlmonks.org?node_id=1010103


in reply to How we can do regex of this?

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.

Replies are listed 'Best First'.
Re^2: How we can do regex of this?
by xMiDgArDx (Initiate) on Dec 23, 2012 at 16:14 UTC
    I get your code and modified some parts to my script, and now work fine!!! Thank you for the help! =)