in reply to Simple regex question

You can accomplish this fairly easily using negative Look Around Assertions.

#!/usr/bin/perl use strict; use warnings; my $test_string = "foo l?est sé?jou ? foo "; $test_string =~ s/(?<!\s)\?(?!\s)/'/sig; print "$test_string\n";

The expression will fail when the character before ((?<!...)) the question mark is white space and when the character after ((?!...)) the question mark is white space. This will substitute if there is a leading or trailing question mark. These are zero-width assertions, so you don't have to worry about capturing. See perlre for details.

Replies are listed 'Best First'.
Re^2: Simple regex question
by ultranerds (Hermit) on Sep 15, 2010 at 14:48 UTC
    Perfect, works a charm. Will have to read up on that page - looks like it has a lot of cool features in that I wasn't aware of :)

    Thanks again

    Andy