regex to NOT match?

Discussion in 'Ruby' started by Ruby Baby, Jan 10, 2004.

  1. Ruby Baby

    Ruby Baby Guest

    Sorry it seems like the smallest thing, but I'm stuck on this.

    I'm trying to make a simple regex to strip away characters that are NOT certain characters I name.
    I'm unclear on what Ruby uses as a "NOT" match in regex, like ! is for !=
    (Or if it uses the ^, where does it put it?)

    EXAMPLE:

    myString = 'I want only the letters a and t and period. When done it should look like atttaatta.tatttaatta..'

    # this doesn't work of course:
    puts myString.gsub(!/at\./, '')

    Can someone show me where to find the rule on this?
    I couldn't find it in any documentation, or Ruby Way or Programming Ruby books.

    Thanks!
    Ruby Baby, Jan 10, 2004
    #1
    1. Advertising

  2. On Saturday 10 January 2004 21:57, Ruby Baby wrote:
    > Sorry it seems like the smallest thing, but I'm stuck on this.
    >
    > I'm trying to make a simple regex to strip away characters that are NOT
    > certain characters I name. I'm unclear on what Ruby uses as a "NOT" match
    > in regex, like ! is for != (Or if it uses the ^, where does it put it?)
    >
    > EXAMPLE:
    >
    > myString = 'I want only the letters a and t and period. When done it
    > should look like atttaatta.tatttaatta..'
    >
    > # this doesn't work of course:
    > puts myString.gsub(!/at\./, '')


    puts myString.gsub(/[^at.]/,'')

    Alex

    --
    I've got a COUSIN who works in the GARMENT DISTRICT ...
    Alexander Kellett, Jan 10, 2004
    #2
    1. Advertising

  3. Ruby Baby

    Martin Weber Guest

    On Sun, Jan 11, 2004 at 05:57:17AM +0900, Ruby Baby wrote:
    > Sorry it seems like the smallest thing, but I'm stuck on this.
    >
    > I'm trying to make a simple regex to strip away characters that are NOT certain characters I name.
    > I'm unclear on what Ruby uses as a "NOT" match in regex, like ! is for !=
    > (Or if it uses the ^, where does it put it?)
    >
    > EXAMPLE:
    >
    > myString = 'I want only the letters a and t and period. When done it should look like atttaatta.tatttaatta..'
    >
    > # this doesn't work of course:
    > puts myString.gsub(!/at\./, '')


    puts myString.gsub(/[^at.]/, '')

    -Martin
    Martin Weber, Jan 10, 2004
    #3
  4. On Saturday 10 January 2004 23:46, GGarramuno wrote:
    > What you want is this:
    >
    > myString.gsub(/[^at\.]/, '')


    \ is unneeded in char class afaik

    > The brackets create a character class, and the ^ is the negative of...
    > If you wanted to deal with full words, instead of single characters, you
    > would have to use the (?!word|word) construct.


    just fyi
    useful ruby specific docs are found by
    googling for "ruby quickref"

    :)

    Alex

    --
    Every man is as God made him, ay, and often worse.
    -- Miguel de Cervantes
    Alexander Kellett, Jan 10, 2004
    #4
  5. Ruby Baby wrote:
    > I couldn't find it in any documentation, or Ruby Way or Programming Ruby books.


    Yes, it would be nice if these two excellent Ruby books explain/tutor
    more about regexp, especially for beginners. As a comparison, regexp
    receives quite a thorough explanation in many Perl books. Not all Ruby
    users come from Perl, mind you. :)

    --
    dave
    David Garamond, Jan 11, 2004
    #5
  6. "Alexander Kellett" <> schrieb im Newsbeitrag
    news:...
    > On Saturday 10 January 2004 21:57, Ruby Baby wrote:
    > > Sorry it seems like the smallest thing, but I'm stuck on this.
    > >
    > > I'm trying to make a simple regex to strip away characters that are NOT
    > > certain characters I name. I'm unclear on what Ruby uses as a "NOT"

    match
    > > in regex, like ! is for != (Or if it uses the ^, where does it put it?)
    > >
    > > EXAMPLE:
    > >
    > > myString = 'I want only the letters a and t and period. When done it
    > > should look like atttaatta.tatttaatta..'
    > >
    > > # this doesn't work of course:
    > > puts myString.gsub(!/at\./, '')

    >
    > puts myString.gsub(/[^at.]/,'')


    This is likely a bit more efficient since it does less replacement
    operations:

    myString.gsub!( /[^at.]+/, '' )

    Regards

    robert
    Robert Klemme, Jan 11, 2004
    #6
  7. David Garamond wrote:
    > Ruby Baby wrote:
    >
    >> I couldn't find it in any documentation, or Ruby Way or Programming
    >> Ruby books.

    >
    > Yes, it would be nice if these two excellent Ruby books explain/tutor
    > more about regexp, especially for beginners. As a comparison, regexp
    > receives quite a thorough explanation in many Perl books. Not all Ruby
    > users come from Perl, mind you. :)
    >


    If you'd like to read some really serious stuff about regexes, I
    recommend Jeffrey Friedl's "Masterning Regular Expressions" by O'Reilly.
    The current edition covers Ruby too, if I remember correctly - I 'only'
    have the 1st edition, but even that one is quite a help in understanding
    regexes in general.

    Happy regexing

    Stephan


    --
    "It's POLYMORPHIC!!!"
    A fromer colleague
    Stephan Kämper, Jan 11, 2004
    #7
  8. Ruby Baby wrote:

    > EXAMPLE:
    > myString = 'I want only the letters a and t and period. When done it should look like atttaatta.tatttaatta..'


    irb(main):002:0> myString.tr("^at.", "")
    => "attttaata.tatttaatta.tatttaatta.."

    Please notice that in regexps there is no general and easy way to not
    match a sub-pattern. (It's however possible to match stuff which isn't
    in a character class with [^abc] and there's exotic stuff like negative
    look-ahead which are very complex and not worth the effort most of the
    time.)

    > Thanks!


    Regards,
    Florian Gross
    Florian Gross, Jan 11, 2004
    #8
  9. On Sun, 11 Jan 2004 19:32:54 +0900, David Garamond wrote:

    > Ruby Baby wrote:
    >> I couldn't find it in any documentation, or Ruby Way or Programming Ruby books.

    >
    > Yes, it would be nice if these two excellent Ruby books explain/tutor
    > more about regexp, especially for beginners. As a comparison, regexp
    > receives quite a thorough explanation in many Perl books. Not all Ruby
    > users come from Perl, mind you. :)


    For many examples which exercises many aspects of Ruby's regexp engine, see
    http://rubyforge.org/cgi-bin/viewcv...=aeditor&content-type=text/vnd.viewcvs-markup

    --
    Simon Strandgaard
    Simon Strandgaard, Jan 11, 2004
    #9
  10. Stephan Kämper wrote:
    > David Garamond wrote:
    >
    >> Ruby Baby wrote:
    >>
    >>> I couldn't find it in any documentation, or Ruby Way or Programming
    >>> Ruby books.

    >>
    >>
    >> Yes, it would be nice if these two excellent Ruby books explain/tutor
    >> more about regexp, especially for beginners. As a comparison, regexp
    >> receives quite a thorough explanation in many Perl books. Not all Ruby
    >> users come from Perl, mind you. :)
    >>

    > If you'd like to read some really serious stuff about regexes, I
    > recommend Jeffrey Friedl's "Masterning Regular Expressions" by O'Reilly.
    > The current edition covers Ruby too, if I remember correctly - I 'only'
    > have the 1st edition, but even that one is quite a help in understanding
    > regexes in general.


    Yeah, I've read Jeffrey's book (1st ed too) and the two chapters of the 2nd.

    I was not talking about /me specifically on the previous post though. I
    myself learnt pretty much everything about regex initially from Perl
    tutorials and books (and after that, of course, from trying it out and
    learning from mistakes). But I've seen here that many people do not come
    from Perl background and the abovementioned two Ruby books didn't cover
    regexp in a friendly way for beginners.

    --
    dave
    David Garamond, Jan 11, 2004
    #10
    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. hiwa
    Replies:
    0
    Views:
    627
  2. Replies:
    3
    Views:
    725
    Reedick, Andrew
    Jul 1, 2008
  3. Neil
    Replies:
    3
    Views:
    1,026
    Andreas Leitgeb
    Aug 29, 2009
  4. Sergio Ruiz

    regex does not contain - or no match

    Sergio Ruiz, Feb 17, 2009, in forum: Ruby
    Replies:
    2
    Views:
    145
    Sergio Ruiz
    Feb 17, 2009
  5. David Joseph Bonnici

    Suppression of error messages if a regex does not match

    David Joseph Bonnici, Jun 1, 2005, in forum: Perl Misc
    Replies:
    8
    Views:
    119
    brian d foy
    Jun 2, 2005
Loading...

Share This Page