tags:

views:

160

answers:

5
/^([a-z]:)?\//i

I don't quite understand what the ? in this regex if I had to explain it from what I understood:

Match begin "Group1 is a to z and :" outside ? (which I don't get what its doing) \/ which makes it match / and option /i "case insensitive".

I realize that this will return 0 or 1 not quiet sure why because of the ?

Is this to match directory path or something ?

If I test it:

$var = 'test' would get 0 while $var ='/test'; would get 1 but $var = 'test/' gets 0

So anything that begins with / will get 1 and anything else 0.

Can someone explain this regex in elementary terms to me?

+7  A: 

? will match one or none of the preceding pattern.

?      Match 1 or 0 times

See also: perldoc perlre

Explanation:

/.../i   # case insensitive

^(...)   # match at the beginning of the string

[a-z]:   # one character between 'a' and 'z' followed by a colon

(...)?   # zero or one time of the group, enclosed in ()

So in english: Match anything which begins with a / (slash) or some letter followed by a colon followed by a /. This looks like it matches pathnames across unix and windows, e.g. it would match:

/home/user

and

C:/Applications

etc.

The MYYN
Thanks again for the long explanation that makes it clear now.
Prix
you'`re` welcome -
The MYYN
+7  A: 

It matches a lower- or upper case letter ([a-z] with the i modifier) positioned at the start of the input string (^) followed by a colon (:) all optionally (?), followed by a forward slash \/.

In short:

^          # match the beginning of the input
(          # start capture group 1
  [a-z]    #   match any character from the set {'A'..'Z', 'a'..'z'} (with the i-modifier!)
  :        #   match the character ':'
)?         # end capture group 1 and match it once or none at all
\/         # match the character '/'
Bart Kiers
Very detailed explanation!
elusive
+2  A: 

It looks like it is looking for a "rooted" path. It will successfully match any string that either starts with a forward slash (/test), or a drive letter followed by a colon, followed by a forward slash (c:/test).

Joshua Flanagan
And probably badly, because if your path starts with a letter and a colon, you're likely going to have backslashes instead of slashes.
Wooble
@Joshua Flanagan thanks that explains what i was missing it was actually made for win and linux.
Prix
+1  A: 

Specifically, the question mark makes something optional. It applies to the part in parentheses, which is a letter followed by a colon.

Things that will match:

C:/
a:/
/

(That last item above is why the ? is there)

Things that will not match:

C:
a:
ab:/
a/
Mark Thomas
+8  A: 

See YAPE::Regex::Explain:

#!/usr/bin/perl

use strict; use warnings;

use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new(qr/^([a-z]:)?\//i)->explain;
The regular expression:

(?i-msx:^([a-z]:)?/)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?i-msx:                 group, but do not capture (case-insensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (                        group and capture to \1 (optional
                           (matching the most amount possible)):
----------------------------------------------------------------------
    [a-z]                    any character of: 'a' to 'z'
----------------------------------------------------------------------
    :                        ':'
----------------------------------------------------------------------
  )?                       end of \1 (NOTE: because you're using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \1)
----------------------------------------------------------------------
  /                        '/'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
Sinan Ünür
Using regexes to decode regexes... that'll take some getting used to.
Zaid