Scanning a String

Discussion in 'Ruby' started by basi, Apr 28, 2005.

  1. basi

    basi Guest

    Hello,
    How do I scan a string for a pattern and process it when found? For
    example:

    for all occurrences of two vowel letters in succession, insert the
    apostrophe between the vowels.

    Thus:

    breakground > bre'akgro'und

    thank you for your help!
    basi
    basi, Apr 28, 2005
    #1
    1. Advertising

  2. On 4/27/05, basi <> wrote:
    > Hello,
    > How do I scan a string for a pattern and process it when found? For
    > example:
    >
    > for all occurrences of two vowel letters in succession, insert the
    > apostrophe between the vowels.


    some_string.gsub(/([aeiou])([aeiou])/) {|match| $1 + "'" + $2}
    Nicholas Seckar, Apr 28, 2005
    #2
    1. Advertising

  3. Hi --

    On Thu, 28 Apr 2005, Nicholas Seckar wrote:

    > On 4/27/05, basi <> wrote:
    >> Hello,
    >> How do I scan a string for a pattern and process it when found? For
    >> example:
    >>
    >> for all occurrences of two vowel letters in succession, insert the
    >> apostrophe between the vowels.

    >
    > some_string.gsub(/([aeiou])([aeiou])/) {|match| $1 + "'" + $2}


    (You don't really need the block arg (match) :)

    Just to add a possible variant: depending how you want to handle three
    or more vowels in a row, you might want to do:

    pious".gsub(/([aeiou])(?=[aeiou])/) { "#{$1}'#{$2}" }

    which will result in: pi'o'us

    (I thought there was a [[:vowel:]] POSIX character class but
    apparently not.)


    David

    --
    David A. Black
    David A. Black, Apr 28, 2005
    #3
  4. basi

    basi Guest

    David,
    Yes, three vowels in a row should be handled as you indicated.
    Thank you, and to Nicholas Seckar as well, for the quick reply.

    I think I should finally bite the bullet and learn regex.

    Basi
    basi, Apr 28, 2005
    #4
  5. On 4/27/05, David A. Black <> wrote:
    > (You don't really need the block arg (match) :)


    Good point :)

    > pious".gsub(/([aeiou])(?=[aeiou])/) { "#{$1}'#{$2}" }


    We could also do away with the block altogether and use
    "pious".gsub(/([aeiou])(?=[aeiou])/, '\1\'\2')

    Although I don't know if one approach is preferred to the other.
    Nicholas Seckar, Apr 28, 2005
    #5
  6. "Nicholas Seckar" <> schrieb im Newsbeitrag
    news:...
    > On 4/27/05, David A. Black <> wrote:
    > > (You don't really need the block arg (match) :)

    >
    > Good point :)
    >
    > > pious".gsub(/([aeiou])(?=[aeiou])/) { "#{$1}'#{$2}" }

    >
    > We could also do away with the block altogether and use
    > "pious".gsub(/([aeiou])(?=[aeiou])/, '\1\'\2')
    >
    > Although I don't know if one approach is preferred to the other.


    Yes, it's preferred because it's faster. Although your variant works, I
    usually prefer to put the correct number of backslashes in there:

    "pious".gsub(/([aeiou])(?=[aeiou])/, '\\1\'\\2')

    Kind regards

    robert
    Robert Klemme, Apr 28, 2005
    #6
  7. On Thu, 28 Apr 2005, David A. Black wrote:

    > Just to add a possible variant: depending how you want to handle three
    > or more vowels in a row, you might want to do:
    >
    > pious".gsub(/([aeiou])(?=[aeiou])/) { "#{$1}'#{$2}" }
    >
    > which will result in: pi'o'us


    I managed to omit the " before pious... and also, I didn't actually
    need #{$2} in there. In fact, $2 is nil (because ?= isn't captured).
    So....

    "pious".gsub(/([aeiou])(?=[aeiou])/) { "#{$1}'" }


    David

    --
    David A. Black
    David A. Black, Apr 28, 2005
    #7
  8. Hi --

    On Thu, 28 Apr 2005, Robert Klemme wrote:

    >
    > "Nicholas Seckar" <> schrieb im Newsbeitrag
    > news:...
    >> On 4/27/05, David A. Black <> wrote:
    >>> (You don't really need the block arg (match) :)

    >>
    >> Good point :)
    >>
    >>> pious".gsub(/([aeiou])(?=[aeiou])/) { "#{$1}'#{$2}" }

    >>
    >> We could also do away with the block altogether and use
    >> "pious".gsub(/([aeiou])(?=[aeiou])/, '\1\'\2')
    >>
    >> Although I don't know if one approach is preferred to the other.

    >
    > Yes, it's preferred because it's faster. Although your variant works, I
    > usually prefer to put the correct number of backslashes in there:
    >
    > "pious".gsub(/([aeiou])(?=[aeiou])/, '\\1\'\\2')


    Why do you consider that more correct?


    David

    --
    David A. Black
    David A. Black, Apr 28, 2005
    #8
  9. "David A. Black" <> schrieb im Newsbeitrag
    news:pine.LNX.4.61.0504280320190.13336@wobblini...
    > Hi --
    >
    > On Thu, 28 Apr 2005, Robert Klemme wrote:
    >
    > >
    > > "Nicholas Seckar" <> schrieb im Newsbeitrag
    > > news:...
    > >> On 4/27/05, David A. Black <> wrote:
    > >>> (You don't really need the block arg (match) :)
    > >>
    > >> Good point :)
    > >>
    > >>> pious".gsub(/([aeiou])(?=[aeiou])/) { "#{$1}'#{$2}" }
    > >>
    > >> We could also do away with the block altogether and use
    > >> "pious".gsub(/([aeiou])(?=[aeiou])/, '\1\'\2')
    > >>
    > >> Although I don't know if one approach is preferred to the other.

    > >
    > > Yes, it's preferred because it's faster. Although your variant works,

    I
    > > usually prefer to put the correct number of backslashes in there:
    > >
    > > "pious".gsub(/([aeiou])(?=[aeiou])/, '\\1\'\\2')

    >
    > Why do you consider that more correct?


    Although both produce the same:

    >> p '\1'

    "\\1"
    => nil
    >> p '\\1'

    "\\1"
    => nil

    because Ruby is so kind to take "\1" literally (i.e not using the
    backslash as escaping char because "\1" is not an escaping sequence). I
    prefer to explicitely escape the backslash and put the "1" in there
    literally. IMHO it's more fail safe when changes are done (especially
    when changing single quotes to double quotes, see below).

    Consider also

    >> p "\n"

    "\n"
    => nil
    >> p "\\n"

    "\\n"
    => nil

    >> p "\1"

    "\001"
    => nil
    >> p "\\1"

    "\\1"
    => nil

    Might be overly cautious but this is the kind of error that bites you and
    if you're not lucky it can take quite some time to figure what's going on
    here.

    Kind regards

    robert
    Robert Klemme, Apr 28, 2005
    #9
  10. Hi --

    On Thu, 28 Apr 2005, Robert Klemme wrote:

    >
    > "David A. Black" <> schrieb im Newsbeitrag
    > news:pine.LNX.4.61.0504280320190.13336@wobblini...
    >> Hi --
    >>
    >> On Thu, 28 Apr 2005, Robert Klemme wrote:
    >>> Yes, it's preferred because it's faster. Although your variant works,

    > I
    >>> usually prefer to put the correct number of backslashes in there:
    >>>
    >>> "pious".gsub(/([aeiou])(?=[aeiou])/, '\\1\'\\2')

    >>
    >> Why do you consider that more correct?

    >
    > Although both produce the same:
    >
    >>> p '\1'

    > "\\1"
    > => nil
    >>> p '\\1'

    > "\\1"
    > => nil
    >
    > because Ruby is so kind to take "\1" literally (i.e not using the
    > backslash as escaping char because "\1" is not an escaping sequence). I
    > prefer to explicitely escape the backslash and put the "1" in there
    > literally. IMHO it's more fail safe when changes are done (especially
    > when changing single quotes to double quotes, see below).


    If you're talking about double quotes then clearly it has to be "\\1".
    I meant specifically in the case of single quotes. Anyway, it's not a
    big deal -- I was just curious.



    David

    --
    David A. Black
    David A. Black, Apr 28, 2005
    #10
  11. "David A. Black" <> schrieb im Newsbeitrag
    news:pine.LNX.4.61.0504280509220.30582@wobblini...
    > Hi --
    >
    > On Thu, 28 Apr 2005, Robert Klemme wrote:
    >
    > >
    > > "David A. Black" <> schrieb im Newsbeitrag
    > > news:pine.LNX.4.61.0504280320190.13336@wobblini...
    > >> Hi --
    > >>
    > >> On Thu, 28 Apr 2005, Robert Klemme wrote:
    > >>> Yes, it's preferred because it's faster. Although your variant

    works,
    > > I
    > >>> usually prefer to put the correct number of backslashes in there:
    > >>>
    > >>> "pious".gsub(/([aeiou])(?=[aeiou])/, '\\1\'\\2')
    > >>
    > >> Why do you consider that more correct?

    > >
    > > Although both produce the same:
    > >
    > >>> p '\1'

    > > "\\1"
    > > => nil
    > >>> p '\\1'

    > > "\\1"
    > > => nil
    > >
    > > because Ruby is so kind to take "\1" literally (i.e not using the
    > > backslash as escaping char because "\1" is not an escaping sequence).

    I
    > > prefer to explicitely escape the backslash and put the "1" in there
    > > literally. IMHO it's more fail safe when changes are done (especially
    > > when changing single quotes to double quotes, see below).

    >
    > If you're talking about double quotes then clearly it has to be "\\1".
    > I meant specifically in the case of single quotes.


    Well, yes. But quotes don't necessarily stay single through the course of
    a software's lifecycle - with all this dating and matching around... :)
    Sorry, drifting off-topic.

    > Anyway, it's not a
    > big deal -- I was just curious.


    Thought so. I hope I could satisfy your curiosity. :)

    Kind regards

    robert
    Robert Klemme, Apr 28, 2005
    #11
  12. basi

    Saynatkari Guest

    Le 28/4/2005, "Robert Klemme" <> a écrit:
    >
    >"Nicholas Seckar" <> schrieb im Newsbeitrag
    >news:...
    >> On 4/27/05, David A. Black <> wrote:
    >> > (You don't really need the block arg (match) :)

    >>
    >> Good point :)
    >>
    >> > pious".gsub(/([aeiou])(?=[aeiou])/) { "#{$1}'#{$2}" }

    >>
    >> We could also do away with the block altogether and use
    >> "pious".gsub(/([aeiou])(?=[aeiou])/, '\1\'\2')
    >>
    >> Although I don't know if one approach is preferred to the other.

    >
    >Yes, it's preferred because it's faster. Although your variant works, I
    >usually prefer to put the correct number of backslashes in there:
    >
    >"pious".gsub(/([aeiou])(?=[aeiou])/, '\\1\'\\2')


    Single quotes, herr Klemme! '\1\'\2' works fine.

    >Kind regards
    >
    > robert


    E

    --
    template<typename duck>
    void quack(duck& d) { d.quack(); }
    Saynatkari, Apr 28, 2005
    #12
    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. Ajay
    Replies:
    0
    Views:
    529
  2. Ajay
    Replies:
    2
    Views:
    7,358
    Ajay Brar
    Aug 4, 2004
  3. excite
    Replies:
    0
    Views:
    796
    excite
    Nov 2, 2006
  4. aurgathor

    scanning string for ![0..9]

    aurgathor, Mar 19, 2005, in forum: C Programming
    Replies:
    9
    Views:
    333
    Tor Rustad
    Mar 20, 2005
  5. aurgathor

    scanning string for ![0..9]

    aurgathor, Mar 19, 2005, in forum: C Programming
    Replies:
    2
    Views:
    283
Loading...

Share This Page