Regex for whitespace plus vertical bar

R

Robert La ferla

I am trying to quote arguments that have whitespace or a pipe (vertical
bar character = | ) in them. Why doesn't this work? What does work?



if arg.index(/[\s|]/)
...

end
 
D

dblack

Hi --

I am trying to quote arguments that have whitespace or a pipe (vertical
bar character = | ) in them. Why doesn't this work? What does work?



if arg.index(/[\s|]/)
...

end

It seems to work OK:

irb(main):008:0> "abc|".index(/[\s|]/)
=> 3
irb(main):009:0> " abc".index(/[\s|]/)
=> 0

What results are you getting?


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
M

Matthew Smillie

I am trying to quote arguments that have whitespace or a pipe
(vertical
bar character = | ) in them. Why doesn't this work? What does work?



if arg.index(/[\s|]/)

| is a reserved character in regexes for disjunction, i.e. /a|b/
matches a or b, where a and b are arbitrary regexes.

This ought to work for your purposes, I think:

if arg.index(/[\s\|]/)

matthew smillie.
 

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,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top