in reply to perl regexp question excluding strings

With regular expressions you can often choose the easy way or the hard way. Although it's tempting to try and solve the whole problem with a single regular expression, that is often the hard way and the easy way is to take a step back and use multiple simple expressions instead. Consider:

use strict; use warnings; my @strings = split "\n", <<STRS; Context Servername Servername Context Servername DBServer Context Servername BogusServer STRS for my $str (@strings) { next if $str =~ /Context\s+Servername|Servername\s+Context/; next if $str !~ /Servername\s+(\w+)/; print "Matched: $1\n"; }

Prints:

Matched: DBServer
True laziness is hard work

Replies are listed 'Best First'.
Re^2: perl regexp question excluding strings
by bobg2011 (Novice) on Nov 05, 2011 at 02:01 UTC

    Thanks guys, I'll give both of the idea's a go

    The reason I was trying to do it all in one regular expression is because I'm loading my regular expression in from a hash which is built from a configuration file containing different regexp's. A particular regexp is loaded depending on the situation. Looks like I'm going to have to rethink my design...

    cheers.