preg functions: use single or double quotes?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sinister

    preg functions: use single or double quotes?

    The examples in the online manual all seem to use double quotes, e.g. at
    Perform a regular expression search and replace


    Why? (The behavior is different with single quotes, and presumably simpler
    to understand.)


  • Chung Leong

    #2
    Re: preg functions: use single or double quotes?

    When it comes to regular expressions, single quotes don't have a distinct
    advantage, in terms of simpler syntax, over double quotes, since you have to
    escape backslashes in both type of strings. Meanwhile you can do variable
    interpolation in doubly quoted strings. So I think "" has the advantage.

    Uzytkownik "sinister" <sinister@nospa m.invalid> napisal w wiadomosci
    news:nkQNb.2064 7$4P6.17529@nwr ddc01.gnilink.n et...[color=blue]
    > The examples in the online manual all seem to use double quotes, e.g. at
    > http://us3.php.net/preg_replace
    >
    > Why? (The behavior is different with single quotes, and presumably[/color]
    simpler[color=blue]
    > to understand.)
    >
    >[/color]


    Comment

    • John Dunlop

      #3
      Re: preg functions: use single or double quotes?

      sinister wrote:
      [color=blue]
      > The examples in the online manual all seem to use double quotes, e.g. at
      > http://us3.php.net/preg_replace
      >
      > Why?[/color]

      Dunno. You might not be too wrong if you were to ascribe that to
      inertia. Remember that examples are just examples -- illustrations of
      particular methods, if you like -- and aren't intended as best-
      practice guidelines. The Manual doesn't urge you to copy their
      constructs character for character. It's sometimes necessary to
      question authority. ;-)
      [color=blue]
      > (The behavior is different with single quotes, and presumably simpler
      > to understand.)[/color]

      Indeed the behaviour is different, but I don't believe it's that much
      simpler to understand, as long as you know how single- and double-
      quoted strings are parsed differently.



      As regards to the question in your Subject line (the Subject line is
      no substitute for the body of a message; at least yours is
      informative though), which was "preg functions: use single or double
      quotes?", that's up to you. If, for instance, you need variables to
      be parsed in your pattern, then you can't use single-quotes.

      There's a nice example given in the documentation for preg_replace on
      the 1st Nov. 2003. It concerns how to go about matching the two-
      character sequence "\n" (a backslash followed by a lowercase letter
      "n"). Consider the string

      $foo = '\n'

      or,

      $foo = "\\n"

      The sequence "\n" is special in a regular expression, in that the two
      characters stand for a linefeed character. Now, in a single-quoted
      pattern, you must precede the "n" by three backslashes:

      preg_match('`\\ \n`',$foo)

      The reason is that a backslash followed by another backslash results
      in a single literal backslash in single-quoted strings. The Manual
      isn't at all clear on this point IMO, although it does say as much,
      in a roundabout kind of way. If we had just two backslashes, the
      regular expression would only match a newline character, just as
      would happen with a single backslash. (A single and double backslash
      behave identically here because a single backslash followed by a
      letter is not special in single-quoted strings, but a double
      backslash is transformed into a single backslash.)

      In a double-quoted pattern however, we need four backslashes before
      the "n". This is because in double-quoted strings, a backslash
      following a backslash results in a single literal backslash, just as
      in single-quoted strings, but additionally, special "escape
      sequences" are recognised within double-quoted strings. "\n" means a
      linefeed character. To stop "\n" from having that meaning, we need to
      escape the backslash with another backslash. Thus, we must use four
      backslashes in total:

      preg_match("`\\ \\n`",$foo)

      --
      Jock

      Comment

      • sinister

        #4
        Re: preg functions: use single or double quotes?


        "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
        news:Z6OdncalHt KetZXdRVn-ig@comcast.com. ..[color=blue]
        > When it comes to regular expressions, single quotes don't have a distinct
        > advantage, in terms of simpler syntax, over double quotes, since you have[/color]
        to[color=blue]
        > escape backslashes in both type of strings. Meanwhile you can do variable
        > interpolation in doubly quoted strings. So I think "" has the advantage.[/color]

        I agree with what you say. By "simpler" I meant that fewer backslashes are
        needed.
        [color=blue]
        >
        > Uzytkownik "sinister" <sinister@nospa m.invalid> napisal w wiadomosci
        > news:nkQNb.2064 7$4P6.17529@nwr ddc01.gnilink.n et...[color=green]
        > > The examples in the online manual all seem to use double quotes, e.g. at
        > > http://us3.php.net/preg_replace
        > >
        > > Why? (The behavior is different with single quotes, and presumably[/color]
        > simpler[color=green]
        > > to understand.)
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • sinister

          #5
          Re: preg functions: use single or double quotes?


          "John Dunlop" <john+usenet@jo hndunlop.info> wrote in message
          news:MPG.1a723d d6bba185c19897e d@news.freeserv e.net...[color=blue]
          > sinister wrote:
          >[color=green]
          > > The examples in the online manual all seem to use double quotes, e.g. at
          > > http://us3.php.net/preg_replace
          > >
          > > Why?[/color]
          >
          > Dunno. You might not be too wrong if you were to ascribe that to
          > inertia. Remember that examples are just examples -- illustrations of
          > particular methods, if you like -- and aren't intended as best-
          > practice guidelines. The Manual doesn't urge you to copy their
          > constructs character for character. It's sometimes necessary to
          > question authority. ;-)[/color]

          That's an interesting aside. I understand that reference material cannot
          dwell exclusively on best practices, but IMHO they ought to be emphasized.
          [color=blue]
          >[color=green]
          > > (The behavior is different with single quotes, and presumably simpler
          > > to understand.)[/color]
          >
          > Indeed the behaviour is different, but I don't believe it's that much
          > simpler to understand, as long as you know how single- and double-
          > quoted strings are parsed differently.
          >
          > http://www.php.net/manual/en/language.types.string.php
          >
          > As regards to the question in your Subject line (the Subject line is
          > no substitute for the body of a message; at least yours is
          > informative though), which was "preg functions: use single or double
          > quotes?", that's up to you. If, for instance, you need variables to
          > be parsed in your pattern, then you can't use single-quotes.[/color]

          Right. In my example, I didn't need variable interpolation.
          [color=blue]
          > There's a nice example given in the documentation for preg_replace on
          > the 1st Nov. 2003. It concerns how to go about matching the two-
          > character sequence "\n" (a backslash followed by a lowercase letter
          > "n"). Consider the string
          >
          > $foo = '\n'
          >
          > or,
          >
          > $foo = "\\n"
          >
          > The sequence "\n" is special in a regular expression, in that the two
          > characters stand for a linefeed character. Now, in a single-quoted
          > pattern, you must precede the "n" by three backslashes:
          >
          > preg_match('`\\ \n`',$foo)
          >
          > The reason is that a backslash followed by another backslash results
          > in a single literal backslash in single-quoted strings. The Manual
          > isn't at all clear on this point IMO, although it does say as much,[/color]

          In the section on preg_replace, or in another section on quoted strings?
          [color=blue]
          > in a roundabout kind of way. If we had just two backslashes, the
          > regular expression would only match a newline character, just as
          > would happen with a single backslash. (A single and double backslash
          > behave identically here because a single backslash followed by a
          > letter is not special in single-quoted strings, but a double
          > backslash is transformed into a single backslash.)
          >
          > In a double-quoted pattern however, we need four backslashes before
          > the "n". This is because in double-quoted strings, a backslash
          > following a backslash results in a single literal backslash, just as
          > in single-quoted strings, but additionally, special "escape
          > sequences" are recognised within double-quoted strings. "\n" means a
          > linefeed character. To stop "\n" from having that meaning, we need to
          > escape the backslash with another backslash. Thus, we must use four
          > backslashes in total:
          >
          > preg_match("`\\ \\n`",$foo)[/color]

          That's what I meant by more complicated.

          Thanks for the detailed explanation!

          Best,

          S
          [color=blue]
          >
          > --
          > Jock[/color]


          Comment

          • John Dunlop

            #6
            Re: preg functions: use single or double quotes?

            sinister wrote:
            [color=blue]
            > "John Dunlop" <john+usenet@jo hndunlop.info> wrote in message
            > news:MPG.1a723d d6bba185c19897e d@news.freeserv e.net...
            >[color=green]
            > > Dunno. You might not be too wrong if you were to ascribe that to
            > > inertia. Remember that examples are just examples -- illustrations of
            > > particular methods, if you like -- and aren't intended as best-
            > > practice guidelines. The Manual doesn't urge you to copy their
            > > constructs character for character. It's sometimes necessary to
            > > question authority. ;-)[/color]
            >
            > That's an interesting aside. I understand that reference material cannot
            > dwell exclusively on best practices, but IMHO they ought to be emphasized.[/color]

            I agree. I honestly don't know why they've used double-quoted strings
            when they're not making use of the benefits that come with them.
            [color=blue][color=green]
            > > The reason is that a backslash followed by another backslash results
            > > in a single literal backslash in single-quoted strings. The Manual
            > > isn't at all clear on this point IMO, although it does say as much,[/color]
            >
            > In the section on preg_replace, or in another section on quoted strings?[/color]

            In the section on single-quoted strings it says:

            | To specify a literal single quote, you will need to escape it with
            | a backslash (\), like in many other languages. If a backslash needs
            | to occur before a single quote or at the end of the string, you
            | need to double it. Note that if you try to escape any other
            | character, the backslash will also be printed! So usually there is
            | no need to escape the backslash itself.



            It says "Note that if you try to escape any other character, the
            backslash will also be printed!". The prior sentence stated in what
            position a backslash *must* be escaped (with another backslash). It
            doesn't explicitly say that if you escape a backslash anywhere in a
            single-quoted string, only a single backslash is left after parsing.
            But that is what it's saying, implicitly. I'm fairly sure it could be
            worded better. Maybe that's just me though.

            --
            Jock

            Comment

            Working...