# Regex for whitespace plus vertical bar

**URL:** <https://www.ruby-forum.com/t/regex-for-whitespace-plus-vertical-bar/100445>\
**Category:** Ruby\
**Created:** [July 28, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/regex-for-whitespace-plus-vertical-bar/100445 "2006-07-28T22:56:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [July 28, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/regex-for-whitespace-plus-vertical-bar/100445/1 "2006-07-28T22:56:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [July 28, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/regex-for-whitespace-plus-vertical-bar/100445/2 "2006-07-28T23:03:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [July 28, 2006, 11:07pm UTC](https://www.ruby-forum.com/t/regex-for-whitespace-plus-vertical-bar/100445/3 "2006-07-28T23:07:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [July 29, 2006, 1:22am UTC](https://www.ruby-forum.com/t/regex-for-whitespace-plus-vertical-bar/100445/4 "2006-07-29T01:22:50Z")

</div>

Matthew S. wrote:

> | is a reserved character in regexes for disjunction,  
> Not in a character class.

> > “foo|bar” =~ /[|]/  
> > =\> 3

---

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [July 28, 2006, 11:10pm UTC](https://www.ruby-forum.com/t/regex-for-whitespace-plus-vertical-bar/100445/5 "2006-07-28T23:10:59Z")

</div>

On 7/28/06, Robert La ferla [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://blog.casey-sweat.us/)
