开发者

What is this regex doing?

/^([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 = 'te开发者_C百科st' 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?


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
----------------------------------------------------------------------


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 '/'


? 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.


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).


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/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜