#!/usr/bin/perl use strict; use warnings; use 5.016; my @line = ("1+2 blah blah", "leading words 1+2 blah blah", "spaces around arith op: 1 + 2 blah blah", ); for my $line(@line) { chomp $line; my ($foo,$bar) = split /\s*\+\s*/, $line; $foo =~ s/.*([0-9]+).*/$1/; $bar =~ s/.*([0-9]+).*/$1/; say $foo + $bar . " is total at Ln 16"; } __END__ output: 3 is total at Ln 16 3 is total at Ln 16 3 is total at Ln 16