Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
"be consistent"
 
PerlMonks  

Reguar expression

by selva (Scribe)
on Jun 22, 2009 at 10:03 UTC ( #773560=perlquestion: print w/ replies, xml ) Need Help??
selva has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks ,
I need a regular expression to match the following pattern .
It should start with "This" and end with "."
In between it should not contain "first" word .
Ex : "This is first sentence." - No match "This is second sentence." - Match

Comment on Reguar expression
Download Code
Re: Reguar expression
by targetsmart (Curate) on Jun 22, 2009 at 10:06 UTC
    What regex have you tried?? where is your piece of code that needs correction??


    Vivek
    -- 'I' am not the body, 'I' am the 'soul', which has no beginning or no end, no attachment or no aversion, nothing to attain or lose.
Re: Reguar expression
by perliff (Monk) on Jun 22, 2009 at 10:31 UTC
    You haven't provided much detail...and no code that you have tried. You are much more likely to get more informative help if you post your code and let others see what you have tried. Search the perlmonks site. Its a great resource. (for example regular expressions ) regardless... I'm assuming you have your data in some text file.

    here's the data in a text file...

    This is first sentence. This is second sentence.

    You can try this for a start...

    use strict; open (INP,"data.txt") || die "Cannot open data.txt\n"; while (my $line = <INP>) { if ($line=~/^This\b/i) { # first word This if ($line !~ /\bfirst\b/i) { # without word first if ($line =~ /\.$/) { # . at the end print $line; } } } }
    I have written the comments next to the pattern match so you can see what's happening at each line. If you want to know what the symbols mean, its better to look at a table of regular expression symbols in perl. There is no other short cut. however, this is only one of ways you could do this. I'm sure people can come up with shorter, fancier and even easier ways to do this kind of pattern match. This why you need to post your code, and provide better problem descriptions... so people can suggest different things, and then you learn much more.

    perliff

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

    -with perl on my side

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

Re: Reguar expression
by chomzee (Beadle) on Jun 22, 2009 at 10:33 UTC
    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/);
      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. :)

      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: Reguar expression
by GrandFather (Cardinal) on Jun 22, 2009 at 10:51 UTC

    Aside from not showing any attempt at solving the problem, which has already been mentioned, this has something of the odour of homework. With that in mind the references you need are: perlre, perlretut and possibly perlreref.

    This matching of words is somewhat tricky, firstly due to managing apostrophes (which often look like single quote marks) and secondly due to the fact that often a word you might wish to match is embedded in another word.


    True laziness is hard work
Re: Reguar expression
by perliff (Monk) on Jun 22, 2009 at 11:01 UTC
    Ok, so here's a shorter one, lifting the regex shamelessly from chomzee and perhaps more to the taste of citromatik ? ;) with a text file that has quotes as well...

    "This is first sentence." "This is second sentence."
    new code..
    open (INP,"data.txt") || die "Cannot open data.txt\n"; while (<INP>) { print if (/^\"This\b/i and /\.\"$/ and !/\bfirst\b/i) }

    perliff

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

    -with perl on my side

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

Re: Reguar expression
by AnomalousMonk (Monsignor) on Jun 22, 2009 at 11:37 UTC
    I, too, think the OP smells of homework, but since others have chimed in, here's my contribution of a regex (but it doesn't handle enclosing double-quotes, left as an exercise for the OPer – perhaps another post?):
    >perl -wMstrict -le "my $rx = qr{ \A (?! (?i) .* \bfirst\b ) This .* \. \z }xms; print '----- output -----'; for my $str (@ARGV) { print $str, $str =~ $rx ? ' M' : ' No M', 'atch'; } " "This is first sentence." "This is second sentence." "This is the First Lady." "This is the firstborn." ----- output ----- This is first sentence. No Match This is second sentence. Match This is the First Lady. No Match This is the firstborn. Match

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://773560]
Approved by targetsmart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2014-04-23 22:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (556 votes), past polls