Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > regex to NOT match?

Reply
Thread Tools

regex to NOT match?

 
 
Ruby Baby
Guest
Posts: n/a
 
      01-10-2004
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!


 
Reply With Quote
 
 
 
 
Alexander Kellett
Guest
Posts: n/a
 
      01-10-2004
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 ...

 
Reply With Quote
 
 
 
 
Martin Weber
Guest
Posts: n/a
 
      01-10-2004
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


 
Reply With Quote
 
Alexander Kellett
Guest
Posts: n/a
 
      01-10-2004
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

 
Reply With Quote
 
David Garamond
Guest
Posts: n/a
 
      01-11-2004
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



 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-11-2004

"Alexander Kellett" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> 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

 
Reply With Quote
 
Stephan Kämper
Guest
Posts: n/a
 
      01-11-2004
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

 
Reply With Quote
 
Florian Gross
Guest
Posts: n/a
 
      01-11-2004
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


 
Reply With Quote
 
Simon Strandgaard
Guest
Posts: n/a
 
      01-11-2004
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/viewcvs...viewcvs-markup

--
Simon Strandgaard
 
Reply With Quote
 
David Garamond
Guest
Posts: n/a
 
      01-11-2004
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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
regex bug (comments within regex not as robust) kg.google@olympiakos.com Perl Misc 3 10-27-2005 07:21 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments