regex and lookahead to specific number of chars

Discussion in 'Ruby' started by Adam Akhtar, Feb 25, 2008.

  1. Adam Akhtar

    Adam Akhtar Guest

    If I have a string such as
    "I love ruby, i really do love ruby, oh yes i do blah blah blah"

    and i want to insert an asterix at every 10th char whether its a
    whitespace, digit whatever how do i go about it

    i was trying

    string.gsub!(/[.]{10}/, "*")

    but it doesnt work

    any hints???
    --
    Posted via http://www.ruby-forum.com/.
     
    Adam Akhtar, Feb 25, 2008
    #1
    1. Advertising

  2. Adam Akhtar

    F. Senault Guest

    Le 25 février à 21:28, Adam Akhtar a écrit :

    > and i want to insert an asterix at every 10th char whether its a
    > whitespace, digit whatever how do i go about it
    >
    > i was trying
    >
    > string.gsub!(/[.]{10}/, "*")


    What about string.gsub!(/(.{9})./, '\1*') ?

    Fred
    --
    I know how to hurt I know how to kill I know what to show
    And what to conceal I know when to talk And I know when to touch
    No one ever died from wanting too much
    (Garbage, The World Is Not Enough)
     
    F. Senault, Feb 25, 2008
    #2
    1. Advertising

  3. On 25.02.2008 21:28, Adam Akhtar wrote:
    > If I have a string such as
    > "I love ruby, i really do love ruby, oh yes i do blah blah blah"
    >
    > and i want to insert an asterix at every 10th char whether its a
    > whitespace, digit whatever how do i go about it
    >
    > i was trying
    >
    > string.gsub!(/[.]{10}/, "*")
    >
    > but it doesnt work
    >
    > any hints???


    Remove the square brackets around the dot. The dot is meta only outside
    of them.

    Cheers

    robert
     
    Robert Klemme, Feb 25, 2008
    #3
  4. Adam Akhtar

    Adam Akhtar Guest

    Thanks Fred and Robert for that.

    I dont understand what the \1 does. I know if i remove it it removes all
    the characters before the 10th and if i include it in keeps them. Why is
    this?



    --
    Posted via http://www.ruby-forum.com/.
     
    Adam Akhtar, Feb 27, 2008
    #4
  5. Adam Akhtar

    Adam Akhtar Guest

    oh and a totally newb question but when i try to substitute the asterix
    with a newline \n it just prints it instead of creating a newline, why
    is that???

    i wish there some regex exercises out there instead of the usual
    tutorial only stuff.
    --
    Posted via http://www.ruby-forum.com/.
     
    Adam Akhtar, Feb 27, 2008
    #5
  6. Adam Akhtar

    Mark Bush Guest

    Adam Akhtar wrote:

    > I dont understand what the \1 does. I know if i remove it it removes all
    > the characters before the 10th and if i include it in keeps them. Why is
    > this?


    When you put a regexp in round brackets, what it matches is remembered.
    In a replacement string, you can then refer to them as \1, \2, etc.

    > oh and a totally newb question but when i try to substitute the asterix
    > with a newline \n it just prints it instead of creating a newline, why
    > is that???


    Since the replacement string is in single quotes, \n is not special so
    represents the two characters \ and n. To get a newline, you need to
    put \n in double quotes ("\n"). In double quotes, the backslash quotes
    any character, so \1 would become the character with code 1. So in
    double quotes, the \1 needs to be \\1 so:

    string.gsub!(/(.{9})./, "\\1*")

    for the original example and:

    string.gsub!(/(.{9})./, "\\1\n")

    for the newline example.
    --
    Posted via http://www.ruby-forum.com/.
     
    Mark Bush, Feb 27, 2008
    #6
  7. Adam Akhtar

    F. Senault Guest

    Le 27 février à 23:40, Adam Akhtar a écrit :

    > Thanks Fred and Robert for that.
    >
    > I dont understand what the \1 does. I know if i remove it it removes all
    > the characters before the 10th and if i include it in keeps them. Why is
    > this?


    It's a reference to the first parenthesied group. To decompose the
    expression :

    ( => begin group 1
    .{9} => find exactly 9 characters
    ) => end group 1
    . => followed by exactly one character

    I replace that whole group by the first group (my 9 characters) and a
    star.

    There are best ways to do it (especially if you're manipulating very big
    strings, I think), but I think it's the clearest.

    Fred
    --
    Full-size scottish highland pipes are loud enough to fall into the
    category of 'weaponry', especially if they're played poorly, but it's an
    analogue of heavy metal if they're played well.
    (Gary S. Callison in the SDM)
     
    F. Senault, Feb 28, 2008
    #7
    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. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,342
    Tim Rentsch
    Sep 23, 2005
  2. Jelle Smet
    Replies:
    2
    Views:
    763
    Helmut Jarausch
    Nov 23, 2009
  3. MRAB
    Replies:
    0
    Views:
    940
  4. Replies:
    2
    Views:
    213
    Anno Siegel
    Dec 29, 2004
  5. shifty
    Replies:
    9
    Views:
    225
Loading...

Share This Page