Regex for whitespace plus vertical bar

Discussion in 'Ruby' started by Robert La ferla, Jul 28, 2006.

  1. 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/.
    Robert La ferla, Jul 28, 2006
    #1
    1. Advertising

  2. Robert La ferla

    Guest

    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.
    , Jul 28, 2006
    #2
    1. Advertising

  3. 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.
    Matthew Smillie, Jul 28, 2006
    #3
  4. Robert La ferla

    Jason Sweat Guest

    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/
    Jason Sweat, Jul 28, 2006
    #4
  5. 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
    William James, Jul 29, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    0
    Views:
    3,541
  2. The LoxFather

    C plus plus vs C Sharp

    The LoxFather, Aug 9, 2003, in forum: C++
    Replies:
    23
    Views:
    9,206
  3. The LoxFather

    C plus plus vs C Sharp

    The LoxFather, Aug 9, 2003, in forum: C Programming
    Replies:
    23
    Views:
    1,385
  4. Rahul
    Replies:
    9
    Views:
    419
    Andrey Tarasevich
    Mar 25, 2008
  5. furqan shaikh

    c plus plus code comparator

    furqan shaikh, Nov 7, 2008, in forum: C++
    Replies:
    6
    Views:
    816
    red floyd
    Nov 12, 2008
Loading...

Share This Page