regular expression

Discussion in 'Ruby' started by David Nguyen, Aug 6, 2008.

  1. David Nguyen

    David Nguyen Guest

    Hi,
    I need a regular expression for a filename :
    it must be composed with the following letters :

    a to z
    A to Z
    0 to 9
    dash sign -
    underscore sign _

    then how can I compare a string with it :

    mystring =~ /myregex/ #return true or false

    does it works like that ?
    --
    Posted via http://www.ruby-forum.com/.
     
    David Nguyen, Aug 6, 2008
    #1
    1. Advertising

  2. On Aug 6, 2008, at 10:26 AM, David Nguyen wrote:

    > Hi,
    > I need a regular expression for a filename :
    > it must be composed with the following letters :
    >
    > a to z
    > A to Z
    > 0 to 9
    > dash sign -
    > underscore sign _
    >
    > then how can I compare a string with it :
    >
    > mystring =~ /myregex/ #return true or false
    >
    > does it works like that ?


    Yes. Although String#=~ returns the position the match starts, or nil
    if there is no match. If the right-hand-side is not a Regexp, invokes
    obj.=~, passing str as an argument. The default =~ in Object returns
    false.

    Since a Fixnum is not nil or false, it will be "true" and both nil and
    false will be "false".

    The only other issue is that the dash (hyphen-minus; '-') is special
    inside a character class of regular expressions. For example, if you
    wanted to match a dash or digit, you could use /[-0-9]/ so the -
    appearing first is not treated as a character range operation.

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Aug 6, 2008
    #2
    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. Keith-Earl
    Replies:
    1
    Views:
    468
    Mary Chipman
    Jun 15, 2004
  2. VSK
    Replies:
    2
    Views:
    2,354
  3. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    872
    Alan Moore
    Dec 2, 2005
  4. GIMME
    Replies:
    3
    Views:
    12,024
    vforvikash
    Dec 29, 2008
  5. Noman Shapiro
    Replies:
    0
    Views:
    245
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page