in reply to Reguar expression

That's not very complicated:
#!/usr/bin/perl $_ = "This is first sentence."; print "1st: match\n" if (/^This/ and /\.$/ and !/first/); $_ = "This is second sentence."; print "2nd: match\n" if (/^This/ and /\.$/ and !/first/);

Replies are listed 'Best First'.
Re^2: Reguar expression
by citromatik (Curate) on Jun 22, 2009 at 10:45 UTC
    That's not very complicated:

    Well, maybe, but you are not asking the OP. That is: "I need a regular expression to match the following pattern.". You are using 2. (and perliff above uses 3!).

    Update: Solving the problem with 2+ regexps is more or less trivial, but using just 1 is far more interesting. Here is a solution using look-ahead assertions (see perlre):

    my $re = qr{ ^ # match at the beginning. (?! # Look-ahead assertion: Fail if the next matc +hes .* first # match "first" everywhere except at the begi +nning of the string ) # end of look-ahead assertion This .* \. # Match "This" (at the beginning), followed b +y anything and a dot $ # Match the end of a string. }x; # Allow these comments

    citromatik

Re^2: Reguar expression
by perliff (Monk) on Jun 22, 2009 at 10:45 UTC
    Nifty work chomzee, I like it, but considering mr. selva has not cared to provide any details into his own problems... and considering unknown issues with his unknown data... I would feel a wee bit safer with this...
    $_ = "This is first sentence."; print "1st: match\n" if (/^This/i and /\.$/ and !/\bfirst\b/); $_ = "This is second sentence."; print "2nd: match\n" if (/^This/i and /\.$/ and !/\bfirst\b/);

    perliff

    ----------------------

    -with perl on my side

    "If you look at the code too long, the code also looks back at you"

      That's true, it certainly depends on data and some details that OP didn't give (so your hints are reasonable and useful).

      And it's still three regular expressions not one, but who cares -- the code does the job well. :)