RE's question..

Discussion in 'Perl Misc' started by howa, Jan 22, 2006.

  1. howa

    howa Guest

    hi guy,

    i have a very simple question about RE:

    RE: /.*/

    String: "this is a test"

    why there is a 2 matches, rather than just 1 ?

    thanks.

    howa
     
    howa, Jan 22, 2006
    #1
    1. Advertising

  2. howa

    MSG Guest

    howa wrote:
    > hi guy,
    >
    > i have a very simple question about RE:
    >
    > RE: /.*/

    it matches almost any thing, be it an one letter string,
    or a million character string in one line, even an empty
    stirng. /.*/ is pretty much never used by itself.
    Please read more about regex quantifier, things
    like * + ? and make sure you understand their difference.

    >
    > String: "this is a test"
    >
    > why there is a 2 matches, rather than just 1 ?

    Are your printing something here like return code? I don't
    think people can understand what you mean without seeing
    your program code.
    >
    > thanks.
    >
    > howa


    Also please always do
    use strict;
    use warnings;
     
    MSG, Jan 22, 2006
    #2
    1. Advertising

  3. howa wrote:
    > i have a very simple question about RE:
    >
    > RE: /.*/
    >
    > String: "this is a test"
    >
    > why there is a 2 matches, rather than just 1 ?


    Do you mean that e.g.

    print scalar( () = "this is a test" =~ /.*/g ), "\n";

    outputs:
    2

    If you do, it's related to the fact that

    print scalar( () = '' =~ /.*/g ), "\n";

    outputs:
    1

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 22, 2006
    #3
  4. howa

    Guest

    Gunnar Hjalmarsson wrote:
    > howa wrote:
    > > i have a very simple question about RE:
    > >
    > > RE: /.*/
    > >
    > > String: "this is a test"
    > >
    > > why there is a 2 matches, rather than just 1 ?

    >
    > Do you mean that e.g.
    >
    > print scalar( () = "this is a test" =~ /.*/g ), "\n";
    >
    > outputs:
    > 2


    Without 'g', this also prints '1'.

    >
    > If you do, it's related to the fact that
    >
    > print scalar( () = ''" =~ /.*/g ), "\n";
    >
    > outputs:
    > 1
    >


    But, with or without 'g', this prints '1'. This suggests a bug or
    mis-feature IMO
    since the regex is matched against a constant in both cases.

    --
    Charles DeRykus
     
    , Jan 22, 2006
    #4
  5. wrote:
    > Gunnar Hjalmarsson wrote:
    >>howa wrote:
    >>>i have a very simple question about RE:
    >>>
    >>>RE: /.*/
    >>>
    >>>String: "this is a test"
    >>>
    >>>why there is a 2 matches, rather than just 1 ?

    >>
    >>Do you mean that e.g.
    >>
    >> print scalar( () = "this is a test" =~ /.*/g ), "\n";
    >>
    >>outputs:
    >>2

    >
    > Without 'g', this also prints '1'.


    So? Without the /g modifier, the return value of the m// operator is
    either true or false, so those results are not comparable.

    >>If you do, it's related to the fact that
    >>
    >> print scalar( () = ''" =~ /.*/g ), "\n";
    >>
    >>outputs:
    >>1

    >
    > But, with or without 'g', this prints '1'. This suggests a bug or
    > mis-feature IMO
    > since the regex is matched against a constant in both cases.


    No.

    In the first case (with the /g modifier), it first matches the whole
    string, and then the nullstring. In the second case, it simply matches
    the nullstring.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 22, 2006
    #5
  6. howa <> wrote:

    > i have a very simple question about RE:
    >
    > RE: /.*/
    >
    > String: "this is a test"
    >
    > why there is a 2 matches, rather than just 1 ?



    There are not 2 matches there, so then the question becomes:

    What makes you think that there are 2 matches?

    Please post a short and complete program that we can run, that
    demonstrates your problem, and we can surely explain it to you.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 23, 2006
    #6
  7. howa

    robic0 Guest

    On Mon, 23 Jan 2006 00:27:18 +0100, Gunnar Hjalmarsson <> wrote:

    > wrote:
    >> Gunnar Hjalmarsson wrote:
    >>>howa wrote:
    >>>>i have a very simple question about RE:
    >>>>
    >>>>RE: /.*/
    >>>>
    >>>>String: "this is a test"
    >>>>
    >>>>why there is a 2 matches, rather than just 1 ?
    >>>
    >>>Do you mean that e.g.
    >>>
    >>> print scalar( () = "this is a test" =~ /.*/g ), "\n";
    >>>
    >>>outputs:
    >>>2

    >>
    >> Without 'g', this also prints '1'.

    >
    >So? Without the /g modifier, the return value of the m// operator is
    >either true or false, so those results are not comparable.
    >
    >>>If you do, it's related to the fact that
    >>>
    >>> print scalar( () = ''" =~ /.*/g ), "\n";
    >>>
    >>>outputs:
    >>>1

    >>
    >> But, with or without 'g', this prints '1'. This suggests a bug or
    >> mis-feature IMO
    >> since the regex is matched against a constant in both cases.

    >
    >No.
    >
    >In the first case (with the /g modifier), it first matches the whole
    >string, and then the nullstring. In the second case, it simply matches
    >the nullstring.


    Now theres something to worry about.
    If a tree falls in the woods does it make a sound?
     
    robic0, Jan 23, 2006
    #7
  8. howa

    robic0 Guest

    On Sun, 22 Jan 2006 18:06:59 -0600, Tad McClellan <> wrote:

    >howa <> wrote:
    >
    >> i have a very simple question about RE:
    >>
    >> RE: /.*/
    >>
    >> String: "this is a test"
    >>
    >> why there is a 2 matches, rather than just 1 ?

    >
    >
    >There are not 2 matches there, so then the question becomes:
    >
    > What makes you think that there are 2 matches?
    >
    >Please post a short and complete program that we can run, that
    >demonstrates your problem, and we can surely explain it to you.


    Moreover, if there are 2 matches there, how can they be seperately captured?
    If in scalar context the engine reports 2, shouldn't they be able to be captured
    independently? Or if not, how would you know when to decrement the scalar counter?
     
    robic0, Jan 23, 2006
    #8
  9. howa

    Uri Guttman Guest

    >>>>> "r" == robic0 <robic0> writes:

    r> If a tree falls in the woods does it make a sound?

    if you post here, did you use any live neurons?

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Jan 23, 2006
    #9
  10. howa

    Guest

    Gunnar Hjalmarsson wrote:
    > wrote:
    > > Gunnar Hjalmarsson wrote:
    > >>howa wrote:
    > >>>i have a very simple question about RE:
    > >>>
    > >>>RE: /.*/
    > >>>
    > >>>String: "this is a test"
    > >>>
    > >>>why there is a 2 matches, rather than just 1 ?
    > >>
    > >>Do you mean that e.g.
    > >>
    > >> print scalar( () = "this is a test" =~ /.*/g ), "\n";
    > >>
    > >>outputs:
    > >>2

    > >
    > > Without 'g', this also prints '1'.

    >
    > So? Without the /g modifier, the return value of the m// operator is
    > either true or false, so those results are not comparable.
    >
    > >>If you do, it's related to the fact that
    > >>
    > >> print scalar( () = ''" =~ /.*/g ), "\n";
    > >>
    > >>outputs:
    > >>1

    > >
    > > But, with or without 'g', this prints '1'. This suggests a bug or
    > > mis-feature IMO
    > > since the regex is matched against a constant in both cases.

    >
    > No.
    >
    > In the first case (with the /g modifier), it first matches the whole
    > string, and then the nullstring. In the second case, it simply matches
    > the nullstring.
    >


    Yes, that's correct. I didn't recall that empty paren's worked that
    way
    without backref's.

    --
    Charles DeRykus
     
    , Jan 23, 2006
    #10
    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. sean
    Replies:
    1
    Views:
    594
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    658
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    947
    Scott Allen
    Oct 6, 2005
  4. Philip Meyer
    Replies:
    0
    Views:
    422
    Philip Meyer
    Nov 30, 2003
  5. Bit Byte
    Replies:
    1
    Views:
    844
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page