janggirl
janggirl

Reputation: 11

Perl Pattern Matching extracting inside brackets

I really need help with coming up with the pattern matching solution...

If the string is <6>[ 84.982642] Killing the process

How can I extract them into three separate strings... I need one for 6, 84.982642, and Killing the process.. I've tried many things but these brackets and blank spaces are really confusing me and I keep getting the error message

"WARNING: Use of uninitialized value $bracket in pattern match..."

Is there anyway I can somehow write in this way

($num_1, $num_2, $name_process) = split(/[\-,. :;!?()[\]{}]+/);

Not sure how to extract these..

Help Please? Thank you so much

Upvotes: 0

Views: 490

Answers (1)

Jim Garrison
Jim Garrison

Reputation: 86754

Assuming the input is in $_

($num_1, $num_2, $name_process) = /^<(\d+)>\[([^\]]+)\]\s+(.*)$/;

This assumes the first token in the angle brackets is always a number. For a little more generality use

($num_1, $num_2, $name_process) = /^<([^>]+)>\[([^\]]+)\]\s+(.*)$/;

Explanation:

<([^>]+)> - a left-angle-bracket followed one or more characters that are not a right angle-bracket, followed by a right-angle bracket.

\[([^\]]+)\] - a left-bracket followed by one or more characters that are not a right bracket, followed by a right bracket

\s+(.*) - one or more spaces, then capture everything starting with the first non-blank after that.

Upvotes: 3

Related Questions