Help with regexp's

T

Thiago Massa

[Note: parts of this message were removed to make it a legal post.]

Is there any way to a regexp allow any character but the comma?

My regexp is getting ugly like /(\x2E|\x3A|\x27|\x28|\x29|\x2D|\x21|\x2B)*/
and so on(i ommited the real one for the love of god)

I'm even thinking about implementing it myself like

if not comma
pass
else
not_pass
end

isn't there a way to extend the way ruby deals with regexp and make
something like "/everythingbutnotcomma" mean what i want?

I'm sorry of my lack of knowledge about regexps... I'm trying to really
learn it once and for all.

Thanks in advance.
 
R

Robert Klemme

Is there any way to a regexp allow any character but the comma?

Use a negated character class [^,] or use negated matching !~.
My regexp is getting ugly like /(\x2E|\x3A|\x27|\x28|\x29|\x2D|\x21|\x2B)= */
and so on(i ommited the real one for the love of god)

I'm even thinking about implementing it myself like

if not comma
=A0pass
else
=A0not_pass
end

isn't there a way to extend the way ruby deals with regexp and make
something like "/everythingbutnotcomma" mean what i want?

What exactly are you trying to achieve with matching? Do you want to
make sure there is no comma in a sequence or do you want to do
substitutions?
I'm sorry of my lack of knowledge about regexps... I'm trying to really
learn it once and for all.

I highly recommend "Mastering Regular Expressions".

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Is there any way to a regexp allow any character but the comma?

My regexp is getting ugly like /(\x2E|\x3A|\x27|\x28|\x29|\x2D|\x21|\x2B)*/
and so on(i ommited the real one for the love of god)

I'm even thinking about implementing it myself like

if not comma
pass
else
not_pass
end

isn't there a way to extend the way ruby deals with regexp and make
something like "/everythingbutnotcomma" mean what i want?

I'm sorry of my lack of knowledge about regexps... I'm trying to really
learn it once and for all.

Thanks in advance.


Use a negated character class /[^,]/

Here is an example http://www.rubular.com/r/6zUr2HVznL

Everything that matches the regexp gets highlighted. In this case, every
char matches, so it gets highlighted. Except the comma, because it does not
match since the caret in the front of the character class says "match
anything except" Here is more on character classes, if you are interested
http://www.regular-expressions.info/charclass.html
 

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
474,144
Messages
2,570,823
Members
47,369
Latest member
FTMZ

Latest Threads

Top