regex to NOT match?

R

Ruby Baby

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!
 
A

Alexander Kellett

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
 
M

Martin Weber

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
 
A

Alexander Kellett

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
 
D

David Garamond

Ruby said:
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. :)
 
R

Robert Klemme

Alexander Kellett said:
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
 
S

Stephan Kämper

David said:
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
 
F

Florian Gross

Ruby said:
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.)

Regards,
Florian Gross
 
D

David Garamond

Stephan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top