RegEx for punctuation?

Discussion in 'Ruby' started by John Joyce, Aug 2, 2007.

  1. John Joyce

    John Joyce Guest

    Hey all,
    I have a quick question about RegEx, which I don't use often and I
    don't have my reference book handy.
    How do I gsub all non alpha-numeric characters to nothing,
    I know it's something like this:
    some_string.gsub!('something here', '')

    but I can't for the life of me remember the RegEx for this.

    thanks ahead of time

    John Joyce
     
    John Joyce, Aug 2, 2007
    #1
    1. Advertising

  2. John Joyce

    Alex Young Guest

    John Joyce wrote:
    > Hey all,
    > I have a quick question about RegEx, which I don't use often and I don't
    > have my reference book handy.
    > How do I gsub all non alpha-numeric characters to nothing,
    > I know it's something like this:
    > some_string.gsub!('something here', '')

    irb(main):002:0> ",.'abc123".gsub(/[^[:alnum:]]/, '')
    => "abc123"

    --
    Alex
     
    Alex Young, Aug 2, 2007
    #2
    1. Advertising

  3. John Joyce

    John Joyce Guest

    On Aug 2, 2007, at 2:14 PM, Alex Young wrote:

    > John Joyce wrote:
    >> Hey all,
    >> I have a quick question about RegEx, which I don't use often and I
    >> don't have my reference book handy.
    >> How do I gsub all non alpha-numeric characters to nothing,
    >> I know it's something like this:
    >> some_string.gsub!('something here', '')

    > irb(main):002:0> ",.'abc123".gsub(/[^[:alnum:]]/, '')
    > => "abc123"
    >
    > --
    > Alex
    >

    Thanks a million!
    I'm going to have to do more RegEx stuff to get it buried into my brain!

    One more question though, I previously had this to turn spaces into
    underscores
    some_string.gsub(' ','')

    How can do this while removing all non-space characters?

    John Joyce
     
    John Joyce, Aug 2, 2007
    #3
  4. John Joyce

    John Joyce Guest

    On Aug 2, 2007, at 2:26 PM, John Joyce wrote:

    >
    > On Aug 2, 2007, at 2:14 PM, Alex Young wrote:
    >
    >> John Joyce wrote:
    >>> Hey all,
    >>> I have a quick question about RegEx, which I don't use often and
    >>> I don't have my reference book handy.
    >>> How do I gsub all non alpha-numeric characters to nothing,
    >>> I know it's something like this:
    >>> some_string.gsub!('something here', '')

    >> irb(main):002:0> ",.'abc123".gsub(/[^[:alnum:]]/, '')
    >> => "abc123"
    >>
    >> --
    >> Alex
    >>

    > Thanks a million!
    > I'm going to have to do more RegEx stuff to get it buried into my
    > brain!
    >
    > One more question though, I previously had this to turn spaces into
    > underscores
    > some_string.gsub(' ','')
    >
    > How can do this while removing all non-space characters?
    >
    > John Joyce
    >
    >
    >

    Found it. In Peter Cooper's book! (I knew I bought that ebook for a
    reason)
    this is my final version:

    @asset.permalink = @asset.name.downcase.gsub(' ', '_').gsub(/
    \W/,'')

    downcase everything, turn spaces into underscores, then take all non-
    alpha/non-numeric/non-underscore characters out.
    sweet and simple.
    For anyone else looking for it in the archives,
    \w matches all alpha/numeric/underscore characters
    \W matches everything else.

    I could go a little further in the case of something like

    title = 'Untitled #234'

    and do this:

    title = title.downcase.gsub(' ', '_').gsub('#', 'number').gsub(/\W/, '')

    resulting in:
    untitled_number234

    Nice and tidy. Could be a bit further refined to make sure a space is
    prepended to 'number' if no space precedes '#' and to make sure a
    number actually follows '#' in before going to all the trouble.
    If I wanted hard to read, I could even try to squeeze it all into one
    big RegEx, but the thing I've got above will suffice for now.

    cheers,
    John Joyce
     
    John Joyce, Aug 2, 2007
    #4
    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. Chris Leffer

    Regular expression for punctuation

    Chris Leffer, Jul 9, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    474
    Chris Leffer
    Jul 9, 2003
  2. Chris R. Timmons

    Re: Regular expression for punctuation

    Chris R. Timmons, Jul 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    495
    Chris R. Timmons
    Jul 10, 2003
  3. DBLWizard
    Replies:
    10
    Views:
    779
    Brock Allen
    Apr 2, 2005
  4. Replies:
    3
    Views:
    834
    Reedick, Andrew
    Jul 1, 2008
  5. Shiao
    Replies:
    4
    Views:
    1,295
    jhermann
    Nov 19, 2008
Loading...

Share This Page