ã¯ããã«
ãããã«ã¡ã¯ãhirataraã§ãã
ãç§ãåãã¦æ£è¦è¡¨ç¾ã使ã£ãã®ã¯ãPerlã«ããCGIã§ã®æååå¦çã§ãããããããç§ã¯Perlã使ãç¶ããä»ã§ã¯æ£è¦è¡¨ç¾ãªãã®ã³ã¼ãã£ã³ã°ã¯èããããªãã»ã©ãæ£è¦è¡¨ç¾ãå½ããåã®æ©è½ã¨ãã¦æ¥å¸¸çã«ä½¿ã£ã¦ãã¾ããæã¯æ¨æºã§ã¯æ£è¦è¡¨ç¾ããµãã¼ããã¦ããªãã£ãJavaããä»ã§ã¯æ£è¦è¡¨ç¾ããµãã¼ãããããã«ãªãã¾ãããJavaã ãã§ã¯ãªããä»æ¥ã§ã¯ã»ã¨ãã©ã®é«ç´è¨èªã«ã¨ã£ã¦ãæ£è¦è¡¨ç¾ã¯ãªãã¦ã¯ãªããªãæ©è½ã§ããã¨è¨ã£ã¦ãéè¨ã§ã¯ãªãã»ã©ã¡ã¸ã£ã¼ãªæ©è½ã¨ãªã£ã¦ãã¾ãã
ãæ¬è¨äºã§ã¯ããã®æ£è¦è¡¨ç¾ã®èå°è£ã«å ãå½ã¦ã¾ããä¸è¦ããã¨ä½ããã¨ãé£ããããªæ£è¦è¡¨ç¾ã¨ã³ã¸ã³ã§ããããã®æ ¹åºã«ã¯æ°å¦çãªæ¦å¿µãããããã®æ¦å¿µããç¥ã£ã¦ããã°åºç¤ã¨ãªãæ©è½ã®å®è£ ã¯ãããªã«é£ããããã¾ããããã®é£è¼ã§ã¯ãã®æ°å¦çãªæ¦å¿µãPythonã使ã£ã¦è¡¨ç¾ããªãããå®éã«åä½ããæ£è¦è¡¨ç¾ã¨ã³ã¸ã³ãä½ãä¸ãã¾ãã
対象èªè
ãè¨èªã¯Pythonã§æ¸ãã¦ãã¾ãããPythonã®ç¥èããªãæ¹ã§ããèãæ¹ã¯èªãã§ç解ãã¦é ããã¨æãã¾ãã以ä¸ãåæã¨ãã¾ãã
- åºç¤çãªããã°ã©ãã³ã°ãã§ããæ¹
- æ£è¦è¡¨ç¾ã使ããæ¹
ãã¾ãã以ä¸ã®ãããªæ¹ã«èªãã§é ããã¨å¹¸ãã§ãã
- æ£è¦è¡¨ç¾ããã£ã¨ç¥ãããæ¹
- æ å ±ç§å¦åéã«èå³ãããæ¹
- æ£è¦è¡¨ç¾ã¨ã³ã¸ã³ãå®è£ ããå¿ è¦ãããæ¹
æ£è¦è¡¨ç¾ã¨ã¯ä½ã
ããåç¥ã®éããæ£è¦è¡¨ç¾ã¯æååãè©ä¾¡ã»æä½ããããã«ããã°ã©ãã³ã°è¨èªã«ç¨æãããæ©è½ã®ä¸ã¤ã§ãã詳ããã¯ã詳説 æ£è¦è¡¨ç¾ãï¼Jeffrey E.F. Friedlèï¼ããæ£è¦è¡¨ç¾ã®åé¡éãï¼å±±å²¸è³¢æ²»èï¼ãªã©ãèªãã§é ããã®ãä¸çªã§ããã念ã®ããæ£è¦è¡¨ç¾ãã©ã®ãããªãã®ããç°¡åã«å¾©ç¿ãã¦ã¿ã¾ãããã
Perl
ãPerlãRubyã§ã¯ãè¨èªã«æ£è¦è¡¨ç¾ãå©ç¨ããããã®ææ³ãç´æ¥çµã¿è¾¼ã¾ãã¦ãã¾ããPerlã«ããã¦ãã=~ã㯠Binding Operator ã¨å¼ã°ãã左辺ã®æååãå³è¾ºã®æ£è¦è¡¨ç¾ã§å¦çãããããã«ç¨ãã¾ããã¹ã©ãã·ã¥ã®é(/ï½/)ã«æã¾ããé¨åãæ£è¦è¡¨ç¾ã¨ãã¦è¦ãªããã¾ãã以ä¸ã®ç¨ä¾ã§ã¯ $hoge ã®ä¸ã«"[fb]oo"ã«ãããããé¨åæååããããã調ã¹ã¦ãã¾ãã
my $hoge = 'foo'; print "ããããã¾ãã\n" if $hoge =~ /[fb]oo/;
ããªããå¾è¿°ããä»ã®è¨èªã¨éã£ã¦Perlã§ã¯æ£è¦è¡¨ç¾ã®ã³ã³ãã¤ã«çµæãªãã¸ã§ã¯ããæèããã«å©ç¨ããã¤ã³ã¿ãã§ã¼ã¹ã«ãªã£ã¦ã¯ãã¾ããã qr//表è¨ã使ã£ããã /ï½/o ã®ããã«æå¾ã« o ãã¤ããã¨ä»ã®è¨èªã¨åæ§ã«ã³ã³ãã¤ã«çµæã使ãåããã¨ãã§ãã¾ãã
Java
ãJavaã.NETç³»çã®è¨èªã§ã¯ãæ£è¦è¡¨ç¾ã®æ©è½ã¯ã©ã¤ãã©ãªã¨ãã¦æä¾ããã¾ããJavaã§ã¯ãjava.util.regexããã±ã¼ã¸ã«Patternã¨Matcherã¨è¨ãã¯ã©ã¹ãããã¾ããPatternãã³ã³ãã¤ã«ãããæ£è¦è¡¨ç¾ã表ããMatcherãPatternã«å¯¾ãããããã³ã°å®è¡æã®æ å ±ãåãæ±ãã¾ãã
ãMatcherã«ã¯ãåä½ã®å¾®å¦ã«éã find()
ãlookingAt()
ãmatches()
ã¨è¨ã3ã¤ã®ãããã³ã°å¤å®ã¡ã½ãããããã¾ããå¾ã®èª¬æã§ä½¿ãã®ã§ã以ä¸ã®ã³ã¼ãã§ãã®3ã¤ã®éãã確ããã¦ããã¾ãããã
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test{ static public void main(final String[] args){ final Pattern p = Pattern.compile("[fb]oo"); final String hoge1 = "foo"; final String hoge2 = "foo bar"; final String hoge3 = "hoge foo bar"; for(final String str : new String[]{hoge1, hoge2, hoge3}){ final Matcher m = p.matcher(str); System.out.println("ã" + str + "ã®ãããçµæã"); if(m.find() ) System.out.println("findã§ããããã¾ãã"); if(m.lookingAt()) System.out.println("lookingAtã§ããããã¾ãã"); if(m.matches() ) System.out.println("matchesã§ããããã¾ãã"); } } } /* å®è¡çµæ ãfooã®ãããçµæã findã§ããããã¾ãã lookingAtã§ããããã¾ãã matchesã§ããããã¾ãã ãfoo barã®ãããçµæã findã§ããããã¾ãã lookingAtã§ããããã¾ãã ãhoge foo barã®ãããçµæã findã§ããããã¾ãã */
ãå®è¡çµæãã¿ã¦åããéããmatches()
ã¯å®å
¨ããããlookingAt()
ã¯åæ¹ä¸è´ã«ããããããfind()
ã¯é¨åæååã«ãããããã®å¤å®ããã¾ãã
Python
ãæå¾ã«ãä»åã®å®è£ è¨èªã«å½ããPythonã®äºæ ãè¦ã¦ããã¾ããããPythonã®æ£è¦è¡¨ç¾ã®ã©ã¤ãã©ãªã¯ reã¢ã¸ã¥ã¼ã« ã¨è¨ãååã§æä¾ããã¦ãã¾ãã
# -*- coding: utf-8 -*- import re hoge1 = "foo" hoge2 = "foo bar" hoge3 = "hoge foo bar" regexp = re.compile(r"[fb]oo") for str in (hoge1, hoge2, hoge3): print u"ã%sã®ãããçµæã" % hoge1 if regexp.match(str) : print u"matchã§ããããã¾ãã" if regexp.search(str): print u"searchã§ããããã¾ãã" # å®è¡çµæ # # ãfooã®ãããçµæã # matchã§ããããã¾ãã # searchã§ããããã¾ãã # ãfooã®ãããçµæã # matchã§ããããã¾ãã # searchã§ããããã¾ãã # ãfooã®ãããçµæã # searchã§ããããã¾ãã
ãPythonã®æ£è¦è¡¨ç¾ãªãã¸ã§ã¯ãã«ãmatch()
ã¨search()
ã¨è¨ãï¼ç¨®é¡ã®ãããã³ã°ã¡ã½ãããããã¾ãã®ã§ãJavaã¨åæ§ã®æ¯è¼ãè¡ã£ã¦ã¿ã¾ãããå®è¡çµæãè¦ãã¨ã search
ã¡ã½ãããJavaã®find()
ãmatch
ã¡ã½ãããJavaã®lookingAt()
ã«ä¸è´ããã¨è¨ãããã§ãã
ãreã¢ã¸ã¥ã¼ã«ã«ã¯ãä»ã«ãfindall
ãfinditer
ã®ããã«ãæååå
¨ä½ãé£ç¶ãã¦èµ°æ»ãã¦ãããé¨åãä¸åº¦ã«åãåºãã¤ã³ã¿ãã§ã¼ã¹ãç¨æããã¦ãã¾ãã
ãJavaã§ãPythonã§ããåæ¹ä¸è´ã«ããããããããã®ã«å¯¾ãã¦å¾æ¹ä¸è´ã«ãããããã®ã¡ã½ããã¯ç¨æããã¦ãã¾ãããããã¯ãåæ¹ä¸è´ã®æ¹ãæ£è¦è¡¨ç¾ã¨ã³ã¸ã³ã®åçããè¨ã£ã¦å®ç¾æ¹æ³ãèªç¶ã ããã§ããéã«å¾æ¹ä¸è´ï¼ã¤ã¾ãé¨åä¸è´ããããï¼ã¯å¤å°å¼·å¼ãªæ¹æ³……å ·ä½çã«ã¯ãç·å½ããã§ãã¿ã¼ã³ãå½ããæ¹æ³ã§å®è£ ããã¾ãã