Another RegExp question

Discussion in 'Ruby' started by J. mp, Mar 18, 2007.

  1. J. mp

    J. mp Guest

    Hi again, I'm allways here to ask how to use regular expressions.
    what I want now is to replace chars from a string with empty spaces

    sample = "thi's is a tes<>%$#!|t"

    what I want is use sample.sub(\['<>%$#!|]\,"")
    and the outcome shoudl be

    "this is a test"

    But I only can replace the first occurence of the chars specified in the
    reg exp

    How can I replace all occurences?

    Thnaks,

    --
    Posted via http://www.ruby-forum.com/.
     
    J. mp, Mar 18, 2007
    #1
    1. Advertising

  2. Hi --

    On 3/18/07, J. mp <> wrote:
    > Hi again, I'm allways here to ask how to use regular expressions.
    > what I want now is to replace chars from a string with empty spaces
    >
    > sample = "thi's is a tes<>%$#!|t"
    >
    > what I want is use sample.sub(\['<>%$#!|]\,"")
    > and the outcome shoudl be
    >
    > "this is a test"
    >
    > But I only can replace the first occurence of the chars specified in the
    > reg exp
    >
    > How can I replace all occurences?


    Use gsub instead of sub.


    David

    --
    Q. What is THE Ruby book for Rails developers?
    A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
    Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
    A. Ruby Power and Light, LLC (http://www.rubypal.com)
     
    David A. Black, Mar 18, 2007
    #2
    1. Advertising

  3. J. mp

    Harry Guest

    On 3/18/07, J. mp <> wrote:

    > sample = "thi's is a tes<>%$#!|t"
    >
    > what I want is use sample.sub(\['<>%$#!|]\,"")
    > and the outcome shoudl be
    >
    > "this is a test"
    >
    > But I only can replace the first occurence of the chars specified in the
    > reg exp
    >
    > How can I replace all occurences?
    >
    > Thnaks,
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    Will this work ?

    sample = "thi's is a tes<>%$###!!!!!###!|t"

    x = sample.gsub(/[\''<>%$#!\|]/,"")
    puts x

    Harry


    --

    http://www.kakueki.com/ruby/list.html
    Japanese Ruby List Subjects in English
     
    Harry, Mar 18, 2007
    #3
  4. J. mp wrote:
    > what I want is use sample.sub(\['<>%$#!|]\,"")
    > ...
    > But I only can replace the first occurence of the chars specified in the
    > reg exp
    > How can I replace all occurences?


    Use gsub instead of sub.


    --
    NP: Falkenbach - Homeward shore
    Ist so, weil ist so
    Bleibt so, weil war so
     
    Sebastian Hungerecker, Mar 18, 2007
    #4
  5. On 18.03.2007 12:40, J. mp wrote:
    > Hi again, I'm allways here to ask how to use regular expressions.
    > what I want now is to replace chars from a string with empty spaces
    >
    > sample = "thi's is a tes<>%$#!|t"
    >
    > what I want is use sample.sub(\['<>%$#!|]\,"")
    > and the outcome shoudl be
    >
    > "this is a test"
    >
    > But I only can replace the first occurence of the chars specified in the
    > reg exp
    >
    > How can I replace all occurences?


    You can find it in the docs: http://ruby-doc.org/

    Hint: There are sub, sub!, gsub and gsub!.

    Kind regards

    robert
     
    Robert Klemme, Mar 18, 2007
    #5
  6. J. mp

    Harry Guest

    Harry, Mar 18, 2007
    #6
  7. J. mp

    Harry Guest

    On 3/18/07, J. mp <> wrote:
    >
    > sample = "thi's is a tes<>%$#!|t"
    >
    > what I want is use sample.sub(\['<>%$#!|]\,"")
    > and the outcome shoudl be
    >
    > "this is a test"
    >
    > But I only can replace the first occurence of the chars specified in the
    > reg exp
    >
    > How can I replace all occurences?
    >


    But, wouldn't it be easier to do something like this?

    sample = "thi's is a tes<>%$###!!!!!###!|t"
    y = sample.delete("#!%<'>$|")
    puts y

    Harry

    --

    http://www.kakueki.com/ruby/list.html
    Japanese Ruby List Subjects in English
     
    Harry, Mar 19, 2007
    #7
  8. J. mp

    doug meyer Guest

    If you want, you could tell gsub to replace everything that is not a-z
    or A-Z or a white space character

    sample = "thi's is a tes<>%$#!|t"
    sample.gsub(/[^a-zA-Z\s]/,"")
    "this is a test"

    On 3/18/07, Harry <> wrote:
    > On 3/18/07, J. mp <> wrote:
    > >
    > > sample = "thi's is a tes<>%$#!|t"
    > >
    > > what I want is use sample.sub(\['<>%$#!|]\,"")
    > > and the outcome shoudl be
    > >
    > > "this is a test"
    > >
    > > But I only can replace the first occurence of the chars specified in the
    > > reg exp
    > >
    > > How can I replace all occurences?
    > >

    >
    > But, wouldn't it be easier to do something like this?
    >
    > sample = "thi's is a tes<>%$###!!!!!###!|t"
    > y = sample.delete("#!%<'>$|")
    > puts y
    >
    > Harry
    >
    > --
    >
    > http://www.kakueki.com/ruby/list.html
    > Japanese Ruby List Subjects in English
    >
    >
     
    doug meyer, Mar 20, 2007
    #8
    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. Greg Hurrell
    Replies:
    4
    Views:
    181
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    547
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    409
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    217
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    205
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page