regex match on nothing

Discussion in 'Perl Misc' started by John, Mar 4, 2010.

  1. John

    John Guest

    Hi

    my $x='Fred's book';
    or $x="Fred's book";
    or $x-'Freds book';

    if ($x =~ s /Fred('|\')s book/xxxxx/);

    How can I modify this when there is nothing between Fred and s?

    Regards
    John
    John, Mar 4, 2010
    #1
    1. Advertising

  2. John wrote:
    > Hi
    >
    > my $x='Fred's book';
    > or $x="Fred's book";
    > or $x-'Freds book';
    >
    > if ($x =~ s /Fred('|\')s book/xxxxx/);
    >
    > How can I modify this when there is nothing between Fred and s?


    Just put a question mark after the closing parenthesis:

    if ($x =~ s/Fred('|\')?s book/xxxxx/) {


    --
    These are my personal views and not those of Fujitsu Technology Solutions!
    Josef Möllers (Pinguinpfleger bei FTS)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://de.ts.fujitsu.com/imprint.html
    Josef Moellers, Mar 4, 2010
    #2
    1. Advertising

  3. John wrote:
    > Hi
    >
    > my $x='Fred's book';
    > or $x="Fred's book";
    > or $x-'Freds book';
    >
    > if ($x =~ s /Fred('|\')s book/xxxxx/);
    >
    > How can I modify this when there is nothing between Fred and s?


    Add a third alternative, zero-width match:

    if ( $x =~ s/Fred(?:'|'|)s book/xxxxx/ )



    John
    --
    The programmer is fighting against the two most
    destructive forces in the universe: entropy and
    human stupidity. -- Damian Conway
    John W. Krahn, Mar 4, 2010
    #3
  4. John

    John Guest

    "John" <> wrote in message
    news:hmo1kd$glb$...
    > Hi
    >
    > my $x='Fred's book';
    > or $x="Fred's book";
    > or $x-'Freds book';
    >
    > if ($x =~ s /Fred('|\')s book/xxxxx/);
    >
    > How can I modify this when there is nothing between Fred and s?
    >
    > Regards
    > John
    >
    >
    >



    Many thanks. John.
    John, Mar 5, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    635
  2. Replies:
    3
    Views:
    745
    Reedick, Andrew
    Jul 1, 2008
  3. Devin Jeanpierre
    Replies:
    2
    Views:
    448
    Devin Jeanpierre
    Feb 14, 2012
  4. Heckner
    Replies:
    3
    Views:
    113
    Sherm Pendley
    Jul 11, 2005
  5. jwcarlton
    Replies:
    1
    Views:
    464
    Martin Honnen
    Feb 5, 2011
Loading...

Share This Page