Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > regular expression

Reply
Thread Tools

regular expression

 
 
David Nguyen
Guest
Posts: n/a
 
      08-06-2008
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/.

 
Reply With Quote
 
 
 
 
Rob Biedenharn
Guest
Posts: n/a
 
      08-06-2008
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




 
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
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments