æ£è¦è¡¨ç¾ã®ãã£ããã£ã®çµæãåå¾ãããã¨ãã«ã¯ã次ã®ãããªã³ã¼ããæ¸ã:
if (my @capture = '10/17' =~ m!([0-9]{2})/([0-9]{2})!) { # @capture = (10, 17) }
$1ã$2ã使ã£ã¦ãåå¾ã¯ã§ãããããã£ããã£ãå¢ããã¨ãã«$1, $2, $3, $4, ...ã¨å¢ããã¦ããã®ã¯è¦è¡ã§ããã
ãã®ããã«ããã¨ã@captureã«ãã£ããã£ã®çµæãå
¥ãããããããã£ããã£ã®æ¬å¼§ããªãæ£è¦è¡¨ç¾ã®å ´åã@captureã®å¤ã¯ã©ããªãã®ãã
if (my @capture = '10/17' =~ m![0-9]{2}/[0-9]{2}!) { # @capture = (1) }
ãã£ããã£ããã¦ããªãã®ã«ã@captureã«ã¯(1)ã代å
¥ããã¦ããã空ãªã¹ãã¯falseæ±ããªã®ã§ããããã£ãã±ã¼ã¹ã®ããã«(1)ãè¿ãã¿ããã ã
ãããã@captureã¯ç©ºãªã¹ãã§ããã¹ããªã®ã§ãã©ãããã°ãã¾ã解決ã§ããããHokkaido.pm Casual#6ã§è³ªåããã¨ããã@jamadamãããæãã¦ãããã@jamadam++
Marquee/lib/Marquee/Plugin/Router.pm at master · jamadam/Marquee · GitHub
if (my @captures = ($path =~ $regex)) { $cb->(defined $1 ? @captures : ()); last; }
$1ã«ä½ãå ¥ã£ã¦ããªãå ´åã¯ããã£ããã£ãã¦ããªãã¨å¤æãã¦ã空ãªã¹ãã«ãã¦ããã¿ããã ãã§ãã¡ãã£ã¨å¾ ã£ãããã£ããã£ãããå ´åã¨ãããªãå ´åãããæ£è¦è¡¨ç¾ã ã¨ãã©ããªãï¼:
if (my @capture = $filename =~ /.+?(?:\.(.+))?$/) { @capture = () unless defined $1; # @capture is? }
ãã®æ£è¦è¡¨ç¾ã¯ä»¥ä¸ã®éãã«ãããããã
- 'file.jpg'
- @capture = ('jpg');
- 'file.tar.gz'
- @capture = ('tar.gz');
ããã¾ã§ã¯ãåé¡ãªããã£ããã£ã§ããããããããã®æ£è¦è¡¨ç¾ã¯ãã£ããã£ããªãå ´åãèãããã:
- file
- @capture = ();
ãã®å ´åã@captureã(undef)ã«ãªãã¹ãã ã¨æãã®ã§ã$1ã使ã£ãæ¹æ³ã§ã¯ãã¾ã解決ã§ããªããã¨ãåããããªã(undef)ã§ããã¹ãã:
if (my @capture = '10:12 rainy' =~ /(\d+):(\d+)(?::(\d+))?\s+(.+)/) { # @capture = (10, 12, undef, 'rainy') # ãã®å ´åã§ã¯ãundefãä»£å ¥ããã¦ããï¼ }
perldoc perlvarããããæªãããããã¨@aloelightãããè¨ã£ã¦ããã®ã§ãèªã¿ããã£ã¦ã¿ãã¨ããç¹æ®å¤æ°@+ãçºè¦ããã
You can use $#+ to determine how many subgroups were in the last successful match.
$#+ã§ãã£ããã£ã®æ°ãåããã¿ããã ããã£ããã£ã®æ°ãåããã°ããã¨ã¯ç°¡åã§ããã
ã¾ãã@-ã¨ããç¹æ®å¤æ°ããããåãããã«$#-ã¨ããã¨ãã£ããã£ã®æ°ãåããã$#-ã¯ã*ããããã*ãã£ããã£ã®æ°ãè¿ãã$#+ã¯ããããã¦(?:ãã|ããªã)ãã£ããã£ã®æ°ãè¿ãã¨ãã£ãéããããã¿ããã ã
æçµçã«ãä»åã®åé¡ã¯$#+ã使ã£ã¦ããã£ããã£ã®æ°ãè¦ã¦å¤å¥ãããã¨ã«ãããPerlã®ç¹æ®å¤æ°ã¯å¥¥ãæ·±ããã¦ã大åã®ç¹æ®å¤æ°ã¯ç¥ããªããã®ã°ããã§ãã :)
if (my @capture = $filename =~ /.+(?:\.(jpe?g|png|gif))?/) { @capture = () unless $#+; # @capture is? }
- file.jpeg
- @capture = ("jpeg");
- file.png
- @capture = ("png");
- file
- @capture = (undef);
追è¨
æå¾ã®ä¾ã§ããã/.+?(?:\.(.+))?$/
ã«æ¸ãç´ããã¨ãã¦å¿ãã¦ã¾ãããããããã®ã¾ã¾ã®å½¢ã§ä¿®æ£ããã¨ãªãã¨ã/.+?(?:\.(jpe?g|png|gif))?$/
ã¨ãªãã¾ããã(thanks kitsãã: /.+(?:\.(jpe?g|png|gif))?/
㯠.+
ã§æååã®æ«å°¾ã¾ã§ãããããã®ã§ã()
é¨åã§ã¯ä½ããã£ããã£ãããªãã)
()?
ã使ããªãã»ãããããã¨ããã®ã¯ãã£ãããéãã§ããå®éã使ã£ããã¨ãªãã§ã :)