PHP's str_replace ?

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

    PHP's str_replace ?

    In PHP, if I do
    str_replace(arr ay('a', 'e', 'i', 'o', 'u'), '-', $str)
    it'll replace all vowels with a hyphen in string $str.

    Is there some equivalent in Python ?

    Thanks
  • Grzegorz Staniak

    #2
    Re: PHP's str_replace ?

    On 10.09.2008, Anjanesh Lekshminarayana n <mail@anjanesh. netwroted:
    In PHP, if I do
    str_replace(arr ay('a', 'e', 'i', 'o', 'u'), '-', $str)
    it'll replace all vowels with a hyphen in string $str.
    >
    Is there some equivalent in Python ?
    The .translate() method of strings?
    >>import string
    >>mystr = "This is just a test"
    >>transtable = string.maketran s("aeiouy","------")
    >>mystr.transla te(transtable)
    'Th-s -s j-st - t-st'


    GS
    --
    Grzegorz Staniak <gstaniak _at_ wp [dot] pl>
    Nocturnal Infiltration and Accurate Killing

    Comment

    • Matthias Huening

      #3
      Re: PHP's str_replace ?

      Anjanesh Lekshminarayana n (10.09.2008 15:50):
      In PHP, if I do
      str_replace(arr ay('a', 'e', 'i', 'o', 'u'), '-', $str)
      it'll replace all vowels with a hyphen in string $str.
      >
      Is there some equivalent in Python ?
      What about something like this:

      import re
      new_str = re.sub('([aeiou])-', r'\1', str)


      Matthias

      Comment

      • Matthias Huening

        #4
        Re: PHP's str_replace ?

        Matthias Huening (10.09.2008 16:07):
        Anjanesh Lekshminarayana n (10.09.2008 15:50):
        >In PHP, if I do
        >str_replace(ar ray('a', 'e', 'i', 'o', 'u'), '-', $str)
        >it'll replace all vowels with a hyphen in string $str.
        >>
        >Is there some equivalent in Python ?
        >
        What about something like this:
        >
        import re
        new_str = re.sub('([aeiou])-', r'\1', str)
        >
        Sorry - I misinterpreted your question.
        Try this instead:

        import re
        new_str = re.sub('[aeiou]', '-', str)


        matthias

        Comment

        • Grzegorz Staniak

          #5
          Re: PHP's str_replace ?

          On 10.09.2008, Anjanesh Lekshminarayana n <mail@anjanesh. netwroted:
          In PHP, if I do
          str_replace(arr ay('a', 'e', 'i', 'o', 'u'), '-', $str)
          it'll replace all vowels with a hyphen in string $str.
          >
          Is there some equivalent in Python ?
          The .translate() method of strings?
          >>import string
          >>mystr = "This is just a test"
          >>transtable = string.maketran s("aeiouy","------")
          >>mystr.transla te(transtable)
          'Th-s -s j-st - t-st'

          GS
          --
          Grzegorz Staniak <gstaniak _at_ wp [dot] pl>
          Nocturnal Infiltration and Accurate Killing

          Comment

          • Anjanesh Lekshminarayanan

            #6
            Re: PHP's str_replace ?

            Matthias Huening wrote:
            Matthias Huening (10.09.2008 16:07):
            >Anjanesh Lekshminarayana n (10.09.2008 15:50):
            >>In PHP, if I do
            >>str_replace(a rray('a', 'e', 'i', 'o', 'u'), '-', $str)
            >>it'll replace all vowels with a hyphen in string $str.
            >>>
            >>Is there some equivalent in Python ?
            >>
            >What about something like this:
            >>
            >import re
            >new_str = re.sub('([aeiou])-', r'\1', str)
            >>
            >
            Sorry - I misinterpreted your question.
            Try this instead:
            >
            import re
            new_str = re.sub('[aeiou]', '-', str)
            Wow - this is neat. Thanks

            Comment

            • Christian Heimes

              #7
              Re: PHP's str_replace ?

              Anjanesh Lekshminarayana n wrote:
              >import re
              >new_str = re.sub('[aeiou]', '-', str)
              Wow - this is neat. Thanks
              But probably slower and definitely harder to understand. For simple
              problems the str methods are usually faster than a regular expression.

              Christian

              Comment

              • David Thole

                #8
                Re: PHP's str_replace ?

                On Sep 10, 12:20 pm, Christian Heimes <li...@cheimes. dewrote:
                Anjanesh Lekshminarayana n wrote:
                import re
                new_str = re.sub('[aeiou]', '-', str)
                Wow - this is neat. Thanks
                >
                But probably slower and definitely harder to understand. For simple
                problems the str methods are usually faster than a regular expression.
                >
                Christian
                It's true that regular expressions are generally slower, but I
                disagree that it's hard to understand. When dealing with text, I
                think it's an absolute must that programmers know about regular
                expressions. I think this here is an example where even str_replace
                in Python wouldn't have worked well.

                -David
                Index page for the homepage for David Thole/www.thedarktrumpet.com

                Comment

                • Grzegorz Staniak

                  #9
                  Re: PHP's str_replace ?

                  On 10.09.2008, David Thole <dthole@gmail.c omwroted:
                  >new_str = re.sub('[aeiou]', '-', str)
                  Wow - this is neat. Thanks
                  >>
                  >But probably slower and definitely harder to understand. For simple
                  >problems the str methods are usually faster than a regular expression.
                  >
                  It's true that regular expressions are generally slower, but I
                  disagree that it's hard to understand. When dealing with text, I
                  think it's an absolute must that programmers know about regular
                  expressions. I think this here is an example where even str_replace
                  in Python wouldn't have worked well.
                  What about mystring.transl ate(transtable) ?

                  GS
                  --
                  Grzegorz Staniak <gstaniak _at_ wp [dot] pl>
                  Nocturnal Infiltration and Accurate Killing

                  Comment

                  Working...