Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Regex for whitespace plus vertical bar

Reply
Thread Tools

Regex for whitespace plus vertical bar

 
 
Robert La ferla
Guest
Posts: n/a
 
      07-28-2006
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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
dblack@wobblini.net
Guest
Posts: n/a
 
      07-28-2006
Hi --

On Sat, 29 Jul 2006, Robert La ferla wrote:

> 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.

 
Reply With Quote
 
 
 
 
Matthew Smillie
Guest
Posts: n/a
 
      07-28-2006
On Jul 28, 2006, at 21:56, Robert La ferla wrote:

> 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.

 
Reply With Quote
 
Jason Sweat
Guest
Posts: n/a
 
      07-28-2006
On 7/28/06, Robert La ferla <> wrote:
> 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


Seems to work for me:

$ irb
>> re=/[\s|]/

=> /[\s|]/
>> re.match('kldfslkd')

=> nil
>> re.match('df |sdf')

=> #<MatchData:0x4098995c>


Regards,
Jason
http://blog.casey-sweat.us/

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      07-28-2006
Matthew Smillie wrote:
> On Jul 28, 2006, at 21:56, Robert La ferla wrote:
>
> > 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,


Not in a character class.

>> "foo|bar" =~ /[|]/

=> 3

 
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
Re: Splitting text at whitespace but keeping the whitespace in thereturned list MRAB Python 3 01-26-2010 11:36 PM
Structure using whitespace vs logical whitespace cmdrrickhunter@yaho.com Python 10 12-16-2008 03:51 PM
Whitespace where I don't want whitespace! Oli Filth HTML 9 01-17-2005 08:47 PM
Re: disable title bar, status bar, and address bar of a browser window John Hann ASP .Net 0 08-21-2004 05:07 AM
disable title bar, status bar, and address bar of a browser window Matt ASP .Net 0 08-21-2004 03:50 AM



Advertisments