æ£è¦è¡¨ç¾ã®åºæ¬
ç¥èãã¾ã¨ãããã£ãã®ã§ãèãã¦ã¿ã¾ããã
Rubyã§ã®æ£è¦è¡¨ç¾ã¯ã=~æ¼ç®åã§ããããããã©ããå¤å®ããããæååã«å¯¾ãã¦sub(), gsub(), scan()ãªã©ãå¼ã³åºãã¦ç¨ãã¾ãã
å¾è ã«ã¤ãã¦ã¯ã第ä¸å¼æ°ã«æ£è¦è¡¨ç¾ãæå®ãã¾ãã第äºå¼æ°ã¯ãç½®æããã¹ãã§ããå ´åã¨ããããã¯ã§ããå ´åãããã¾ããã詳細ã¯ãã®è¨äºã§ã¯è§¦ãã¾ãã(scan()ã®ä½¿ç¨ä¾ã¯ãã¡ãã¸)ã
ååã¯çè«çãªå 容ãæ´å²ã«ã¤ãã¦èãã¦ãã¾ãããå¾åã¯Rubyã§ã®å ·ä½çãªä½¿ç¨æ³ã«ã¤ãã¦èãã¦ãã¾ãã
ã»æ£è¦è¡¨ç¾ä¸è¦§
ã»å
·ä½ä¾
ã»å
èªã¿ã»å¾èªã¿
ã»ç½®æ
- ãããããæ£è¦è¡¨ç¾ã¨ã¯ãªã«ã§ããããï¼
- æ£è¦è¡¨ç¾ã®æ´å²ãæãã¦ãã ããã
- æ£è¦è¡¨ç¾ã®çè«ã«ã¤ãã¦æãã¦ãã ããã
- æéãªã¼ãããã³ã¨ã¯ä½ã§ããããï¼ãé決å®çã¨æ±ºå®çã¨ã¯ã©ããããã¨ã§ããããï¼
- æ£è¦è¡¨ç¾ã®æç¨æ§ã«ã¤ãã¦ãçç±ãçè«çèæ¯ããåç¥ã§ãããæãã¦ãã ããã
- å ¥åããã¹ããæ½è±¡çã«è¡¨ç¾ãããã¨ã§ãåãããã°ã©ã ã®ãã¸ãã¯ãæ§ã ãªå ·ä½çæååã«é©ç¨ã§ããã¨ãããã¨ã§ãããããã¾ã§ãã¾ãæèãããã¨ããªãã£ãã§ãã
- æ£è¦è¡¨ç¾ã®ä¸è¦§ãæãã¦ããã ãã¾ãããï¼
- ããããã«ã¤ãã¦ãå ·ä½ä¾ããé¡ããã¾ããRubyã®ã³ã¼ãã§ãé¡ããã¾ãã
- ãããã¨ããããã¾ãããã¹ã¦ãå¼ãtrueãè¿ãå ´åã«ãªãã¾ããã
- æ£è¦è¡¨ç¾ã®ãå èªã¿ããå¾èªã¿ãã®è¡¨ç¾ã«ã¤ãã¦ãæãã¦ãã ããã
- å èªã¿ãå¾èªã¿ã¯ãåæã®æ£è¦è¡¨ç¾ã®å®è£ ã«ãå«ã¾ãã¦ããã®ã§ããï¼
- æååã®ç½®æã«ã¤ãã¦ãæãã¦ãã ããã
- æ£è¦è¡¨ç¾ã§é¿ããã¹ããã¨ãªã©ãããã°æãã¦ãã ããã
- éãã£ããã£ã°ã«ã¼ãã®ä½¿ãæ¹ãæãã¦ãã ããã
ãããããæ£è¦è¡¨ç¾ã¨ã¯ãªã«ã§ããããï¼
User
ãããããæ£è¦è¡¨ç¾ã¨ã¯ãªã«ã§ããããï¼
ChatGPT
æ£è¦è¡¨ç¾ï¼Regular Expressionãç¥ãã¦Regexï¼ã¯ãæååã®æ¤ç´¢ãç½®æãæ½åºãªã©ãå¹ççã«è¡ãããã®ãã¿ã¼ã³ãããã³ã°ã®æè¡ã§ããä¸é£ã®æåã¨ç¹æ®ãªè¨å·ã使ã£ã¦ãããã¹ãå
ã§ã®ç¹å®ã®æååã®ãã¿ã¼ã³ãè¨è¿°ãã¾ãããã®è¨è¿°ãç¨ãã¦ãããã°ã©ãã³ã°è¨èªãããã¹ãã¨ãã£ã¿ã¼ãªã©ããè¤éãªæååæä½ãç°¡åãã¤å¹ççã«å®è¡ã§ãã¾ãã
ä¾ãã°ãã¡ã¼ã«ã¢ãã¬ã¹ãé»è©±çªå·ã®ãããªç¹å®ã®å½¢å¼ãæã¤ããã¹ããæ¤ç´¢ããå ´åãæ£è¦è¡¨ç¾ã使ã£ã¦ãã®å½¢å¼ã«ä¸è´ãããã¿ã¼ã³ãå®ç¾©ã§ãã¾ããæ£è¦è¡¨ç¾ã¯å¤ãã®ããã°ã©ãã³ã°è¨èªããã¼ã«ã§ãµãã¼ãããã¦ãããããã¹ããã¼ã¿ã®å¦çã«ããã¦é常ã«å¼·åãªãã¼ã«ã¨ãªã£ã¦ãã¾ãã
æ£è¦è¡¨ç¾ã®æ´å²ãæãã¦ãã ããã
User
æ£è¦è¡¨ç¾ã®æ´å²ãæãã¦ãã ããã
ChatGPT
æ£è¦è¡¨ç¾ã®æ´å²ã¯ã1950年代ã«ããã®ã¼ãã¾ããæ°å¦è
ã¹ãã£ã¼ãã³ã»ã¯ãªã¼ãï¼Stephen Kleeneï¼ããæ£è¦éåï¼regular setsï¼ãã¨ããæ¦å¿µãçºè¡¨ãã¾ãããããã¯ããã種ã®èªåè¨ç®æ©ï¼ãªã¼ãããã³ï¼ãèªèã§ããè¨èªãè¨è¿°ããæ¹æ³ã¨ãã¦ææ¡ããã¾ãããã¯ãªã¼ãã®ä»äºã¯ãå½¢å¼è¨èªçè«ã®åºç¤ãç¯ããå¾ã®ããã¹ãå¦çãã¼ã«ã®çºå±ã«å¤§ããªå½±é¿ãä¸ãã¾ããã
1960年代ã«å ¥ãã¨ãæ£è¦è¡¨ç¾ã¯ã³ã³ãã¥ã¼ã¿ç§å¦ã®åéã§ããå®ç¨çãªãã¼ã«ã¨ãã¦èªèããå§ãã¾ãããç¹ã«ãã±ã³ã»ãã³ãã½ã³ãUNIXãªãã¬ã¼ãã£ã³ã°ã·ã¹ãã ã®ããã«éçºããããã¹ãã¨ãã£ã¿ãedããæ£è¦è¡¨ç¾ããµãã¼ããããã¨ã«ãã£ã¦ããã®å©ç¨ãåºã¾ãã¾ãããããã«ãããããã¹ããã¡ã¤ã«å ã®ãã¿ã¼ã³ãæ¤ç´¢ã»ç½®æãããã¨ãç°¡åã«ãªãã¾ããã
ãã®å¾ãæ£è¦è¡¨ç¾ã¯å¤ãã®ããã°ã©ãã³ã°è¨èªãã¢ããªã±ã¼ã·ã§ã³ã«çµã¿è¾¼ã¾ããããã«ãªããä»æ¥ã§ã¯é常ã«åºç¯å²ã«ãããç¨éã§ä½¿ç¨ããã¦ãã¾ããä¾ãã°ãPerlãPythonãªã©ã®è¨èªã¯ãæ£è¦è¡¨ç¾ãç¨ããããã¹ãå¦çã®æ©è½ãå¼·åãããã¨ã§ç¥ããã¦ãã¾ããæ£è¦è¡¨ç¾ã¯ããã¼ã¿ããªãã¼ã·ã§ã³ããã¼ã¿æ½åºãè¤éãªããã¹ãå¤æãªã©ãå¤å²ã«ãããåéã§éè¦ãªå½¹å²ãæããã¦ãã¾ãã
æ£è¦è¡¨ç¾ã®çè«ã«ã¤ãã¦æãã¦ãã ããã
User
æ£è¦è¡¨ç¾ã®çè«ã«ã¤ãã¦æãã¦ãã ããã
ChatGPT
æ£è¦è¡¨ç¾ã®çè«ã¯ã主ã«å½¢å¼è¨èªçè«ã®ä¸é¨ã¨ãã¦çºå±ãã¦ãã¾ããããã®çè«ã®æ ¹åºã«ã¯ãèªåè¨ç®æ©ï¼ãªã¼ãããã³ï¼çè«ã¨éåçè«ãããã¾ãã以ä¸ã«ä¸»ãªæ¦å¿µãç°¡åã«èª¬æãã¾ãã
1. å½¢å¼è¨èªã¨ãªã¼ãããã³
æ£è¦è¡¨ç¾ã¯ç¹å®ã®å½¢å¼è¨èªãè¨è¿°ããããã«ä½¿ããã¾ããå½¢å¼è¨èªã¯ãç¹å®ã®ã¢ã«ãã¡ãããã®è¨å·ã使ç¨ãã¦æ§ç¯ãããæååã®éåã§ãããªã¼ãããã³ï¼ç¹ã«æéãªã¼ãããã³ï¼ã¯ããããã®è¨èªãèªèããã®ã«ä½¿ãããè¨ç®ã¢ãã«ã§ããæ£è¦è¡¨ç¾ã«ãã£ã¦å®ç¾©ãããè¨èªã¯ããæ£è¦è¨èªãã¨ãã¦ç¥ãããããã¯æéãªã¼ãããã³ã«ãã£ã¦èªèå¯è½ãªè¨èªã®ã¯ã©ã¹ã«å±ãã¾ãã
2. åºæ¬çãªæ§æè¦ç´
æ£è¦è¡¨ç¾ã¯ä»¥ä¸ã®åºæ¬çãªæä½ã¨è¨å·ããæ§æããã¾ãï¼
é£çµ: äºã¤ã®è¡¨ç¾ãç¶ãã¦æ¸ããã¨ã§ãä¸æ¹ã®æååã®ç´å¾ã«ä»æ¹ãç¶ããã¿ã¼ã³ã表ãã¾ãã
é¸æï¼åï¼: ç¸¦æ£ | ãç¨ãã¦è¡¨ãããé¸æè¢ï¼ä¾ãã° a|bï¼ã®ããããä¸ã¤ããããããããã¨ãã§ãã¾ãã
ç¹°ãè¿ã: *ï¼ã¯ãªã¼ãéå ï¼ã +ï¼1å以ä¸ã®ç¹°ãè¿ãï¼ã使ç¨ãã¦ãæåã®ç¹°ãè¿ããã¿ã¼ã³ã表ãã¾ãã
ãªãã·ã§ã³ï¼ï¼ï¼: ç¹å®ã®æåã0åã¾ãã¯1åç¾ãããã¨ã表ãã¾ãã
3. ã¯ãªã¼ãã®å®ç
ã¹ãã£ã¼ãã³ã»ã¯ãªã¼ããæå±ããéè¦ãªå®çã«ããã¨ãããè¨èªãæ£è¦è¨èªã§ããããã®å¿
è¦ååæ¡ä»¶ã¯ããããæéãªã¼ãããã³ã«ãã£ã¦èªèå¯è½ã§ãããã¨ã§ãããã®å®çã¯ãæ£è¦è¡¨ç¾ã¨æéãªã¼ãããã³ã®éã«æ·±ãé¢ä¿ããããã¨ã示ãã¦ãã¾ãã
4. æ£è¦è¡¨ç¾ã®ç価æ§ã¨å¤æ
ä»»æã®æ£è¦è¡¨ç¾ã¯ç価ãªé決å®çæéãªã¼ãããã³ï¼NFAï¼ã«å¤æãããã¨ãã§ãããã®NFAã¯ããã«æ±ºå®çæéãªã¼ãããã³ï¼DFAï¼ã«å¤æãããã¨ãå¯è½ã§ããããã«ãããæ£è¦è¡¨ç¾ã«ãã£ã¦è¨è¿°ããããã¿ã¼ã³ãå¹ççã«ãããã³ã°ããã¢ã«ã´ãªãºã ãå®è£
ã§ãã¾ãã
æ£è¦è¡¨ç¾ã®çè«ã¯ãããã¹ãå¦çã ãã§ãªããã³ã³ãã¥ã¼ã¿ç§å¦ã®å¤ãã®åéã§åºæ¬çãªãã¼ã«ã¨ãã¦æ©è½ãã¦ãã¾ãããã®ç解ã¯ããã°ã©ãã³ã°ã ãã§ãªããæ å ±çè«ãã³ã³ãã¥ã¼ã¿ã®è¨èªå¦çã®æ·±ãç解ã«ãå¯ä¸ãã¾ãã
æéãªã¼ãããã³ã¨ã¯ä½ã§ããããï¼ãé決å®çã¨æ±ºå®çã¨ã¯ã©ããããã¨ã§ããããï¼
User
æéãªã¼ãããã³ã¨ã¯ä½ã§ããããï¼ãé決å®çã¨æ±ºå®çã¨ã¯ã©ããããã¨ã§ããããï¼
ChatGPT
æéãªã¼ãããã³ï¼Finite Automatonï¼ã¯ãå½¢å¼è¨èªãèªèããããã«ç¨ããããè¨ç®ã¢ãã«ã®ä¸ç¨®ã§ãããã®ã¢ãã«ã¯ãæéã®ç¶æ
ãæã¤ãã¨ãç¹å¾´ã§ãç¹å®ã®ã«ã¼ã«ã«åºã¥ãã¦å
¥åæååã«å¯¾ãã¦ç¶æ
é·ç§»ãè¡ãã¾ããæéãªã¼ãããã³ã«ã¯ä¸»ã«äºã¤ã®ã¿ã¤ããããã¾ãï¼æ±ºå®çæéãªã¼ãããã³ï¼DFAï¼ã¨é決å®çæéãªã¼ãããã³ï¼NFAï¼ã§ãã
決å®çæéãªã¼ãããã³ï¼DFAï¼
DFAã§ã¯ãåç¶æ
ã«ããã¦ããå
¥åãä¸ããããå ´åã®é·ç§»å
ã®ç¶æ
ãä¸æã«æ±ºã¾ãã¾ããã¤ã¾ããããç¶æ
ã¨å
¥åè¨å·ã®çµã¿åããã«å¯¾ãã¦ã次ã«é²ãã¹ãç¶æ
ã常ã«1ã¤ã«å®ãããã¦ããã®ã§ããããã«ãããDFAã®åä½ã¯é常ã«äºæ¸¬ãããããå®è£
ã容æã§ãã
é決å®çæéãªã¼ãããã³ï¼NFAï¼
NFAã§ã¯ãããç¶æ
ã§ã®å
¥åè¨å·ã«å¯¾ãã¦è¤æ°ã®é·ç§»å
ãã¾ãã¯é·ç§»å
ãåå¨ããªãå ´åãããã¾ããã¾ããå
¥åãªãã«ç¶æ
ãé·ç§»ãããã¨ï¼Îµé·ç§»ï¼ã許ãå ´åãããã¾ããNFAã¯DFAã¨æ¯ã¹ã¦ããæè»æ§ãé«ãã§ãããã©ã®é·ç§»ããã©ããã®é¸æãå¿
è¦ã«ãªãããã解æãå®è£
ãè¤éã«ãªããã¨ãããã¾ãã
ç価æ§
éè¦ãªç¹ã¨ãã¦ãä»»æã®NFAã«å¯¾ãã¦ç価ãªDFAãæ§ç¯ãããã¨ãå¯è½ã§ãããã®å¤æã¯ããã¹ã¦ã®å¯è½ãªNFAã®ç¶æ
ã®çµã¿åãããæ°ããDFAã®ç¶æ
ã¨ãã¦çµã¿è¾¼ããã¨ã«ãã£ã¦å®ç¾ããã¾ãããã®ããã»ã¹ã¯ç¶æ
æ°ãææ°é¢æ°çã«å¢å ããããã¨ããããããè¨ç®ã³ã¹ããé«ããªããã¨ãããã¾ãããçµæã¨ãã¦å¾ãããDFAã¯ä¸ããããè¨èªãèªèããç¹ã§NFAã¨å
¨ãåçã§ãã
æéãªã¼ãããã³ã¯ãæ£è¦è¨èªã®çè«çãªç 究ã ãã§ãªããããã°ã©ã ã®è¨è¨ããã¼ã¿éä¿¡ãªã©ãå¤ãã®å®ç¨çãªå¿ç¨ãããã¾ãã
æ£è¦è¡¨ç¾ã®æç¨æ§ã«ã¤ãã¦ãçç±ãçè«çèæ¯ããåç¥ã§ãããæãã¦ãã ããã
User
åãå人çã«ãããã°ã©ãã³ã°ãå¦ãã§ä¸çªè¯ãã£ãã¨æãããã¨ã«ãæ£è¦è¡¨ç¾ã«ããããã¹ãå¦çãã§ããããã«ãªã£ããã¨ãæãããã¾ãã
ä»»æã®ããã¹ãã®æ³åæ§ãããæ°ããªããã¹ããçæããããæ³åæ§ãåæ ããã¬ã¤ã¢ã¦ãã«å¤æãããã¨ãã£ããã¨ãã§ãããããåãå 容ã®ããã¹ããããããªã¬ã¤ã¢ã¦ãã§æã¤å¿ è¦ããªããã¨ãç¹ã«æ°ã«å ¥ã£ã¦ãã¾ãã
ãããã£ãæ£è¦è¡¨ç¾ã®æç¨æ§ã«ã¤ãã¦ãçç±ãçè«çèæ¯ããåç¥ã§ãããæãã¦ãã ããã
ChatGPT
æ£è¦è¡¨ç¾ãããã¹ãå¦çã«ããã¦é常ã«ä¾¿å©ãªçç±ã¯ããã®æè»æ§ã¨ãã¯ãã«ãªãã¿ã¼ã³ãããã³ã°æ©è½ã«ããã¾ããããã«ã¯ä»¥ä¸ã®ãããªçè«çèæ¯ãé¢ãã£ã¦ãã¾ãï¼
1. æ½è±¡åã¨åå©ç¨
æ£è¦è¡¨ç¾ã¯ãç¹å®ã®æååãã¿ã¼ã³ãæ½è±¡åãããã¨ãå¯è½ã«ãã¾ããããã«ãããåããã¿ã¼ã³ãç°ãªãããã¹ãã«é©ç¨ãããã¨ãã§ããã³ã¼ãã®åå©ç¨ãä¿é²ããã¾ããä¾ãã°ãé»è©±çªå·ãã¡ã¼ã«ã¢ãã¬ã¹ã¨ãã£ãä¸è¬çãªãã©ã¼ããããèå¥ããããã«ä¸åº¦ä½æããæ£è¦è¡¨ç¾ãä½åº¦ãå©ç¨ãããã¨ãã§ãã¾ãã
2. å¹ççãªããã¹ãå¦ç
æ£è¦è¡¨ç¾ã¨ã³ã¸ã³ã¯é常ãé«åº¦ã«æé©åããã¦ããã大éã®ããã¹ããã¼ã¿ãè¿
éã«å¦çãããã¨ãã§ãã¾ããããã¯ãæéãªã¼ãããã³ã«åºã¥ããå
é¨ã®å®è£
ã«ãããã®ã§ãç¹å®ã®ãã¿ã¼ã³ã«åè´ããããã¹ããå¹ççã«æ¤ç´¢ããæ½åºãããã¨ãå¯è½ã§ãã
3. æè»æ§
æ£è¦è¡¨ç¾ã¯ãé常ã«ãã¤ãããã¯ãªããã¹ããããã³ã°ãå¯è½ã«ãã¾ããæåã®ç¹°ãè¿ããé¸æããªãã·ã§ãã«ãªãããã³ã°ãã°ã«ã¼ãã³ã°ãªã©ãè¤éãªã«ã¼ã«ãçµã¿åããã¦ãã»ã¨ãã©ä»»æã®ããã¹ããã¿ã¼ã³ãè¨è¿°ã§ãã¾ããããã«ãããç°ãªããã©ã¼ããããã¹ã¿ã¤ã«ã®ããã¹ããã¼ã¿ã«å¯¾å¿ããæè»ãªã½ãªã¥ã¼ã·ã§ã³ãæä¾ã§ãã¾ãã
4. ã·ã³ãã«ãªå¤æ´ã§å¤§ããªå¹æ
ä¸åº¦ã«è¤æ°ã®ããã¹ããã¡ã¤ã«ããã¼ã¿ã¹ããªã¼ã ãå¦çããå ´åãæ£è¦è¡¨ç¾ã使ããã¨ã§ãåä¸ã®å¤æ´ã大ããªå½±é¿ãæã¤ããã«ãªãã¾ãããã¨ãã°ããã©ã¼ããããå¤æ´ãããã ãã§ããæ£è¦è¡¨ç¾ãä¿®æ£ãããã¨ã§ç°¡åã«å¯¾å¿ã§ãã¾ãã
æ£è¦è¡¨ç¾ã®ãã®ãããªæç¨æ§ã¯ãããã°ã©ããæ¥å¸¸çã«ç´é¢ããæ§ã ãªããã¹ãå¦ç課é¡ã«å¯¾ãã¦ãå¹æçãã¤å¹ççãªè§£æ±ºçãæä¾ãã¾ãããã®ãããããã°ã©ãã³ã°ã®ã¹ãã«ã¨ãã¦æ¥µãã¦ä¾¡å¤ãé«ãã¨ããã¦ãã¾ãã
å ¥åããã¹ããæ½è±¡çã«è¡¨ç¾ãããã¨ã§ãåãããã°ã©ã ã®ãã¸ãã¯ãæ§ã ãªå ·ä½çæååã«é©ç¨ã§ããã¨ãããã¨ã§ãããããã¾ã§ãã¾ãæèãããã¨ããªãã£ãã§ãã
User
ãããã¨ããããã¾ãã
å ¥åããã¹ããæ½è±¡çã«è¡¨ç¾ãããã¨ã§ãåãããã°ã©ã ã®ãã¸ãã¯ãæ§ã ãªå ·ä½çæååã«é©ç¨ã§ããã¨ãããã¨ã§ããã
ããã¾ã§ãã¾ãæèãããã¨ããªãã£ãã§ãã
ChatGPT
ã¯ããã¾ãã«ãã®éãã§ããæ£è¦è¡¨ç¾ã使ããã¨ã§ãããã°ã©ã ãããæ±ç¨çãã¤å¹ççã«ãããã¨ãã§ãã¾ããããã¯ããã°ã©ã ã®ã¡ã³ããã³ã¹ã容æã«ããæ§ã
ãªå
¥åãã¼ã¿ã«å¯¾ãã¦æè»ã«å¯¾å¿ããè½åãé«ãã¾ãããã¨ãã°ããã°ãã¡ã¤ã«ããç¹å®ã®æ
å ±ãæ½åºããããã¦ã¼ã¶ã¼å
¥åãããã©ã¼ããããæ¤è¨¼ãããããéã«ãæ£è¦è¡¨ç¾ã大å¤å½¹ç«ã¡ã¾ãã
æ£è¦è¡¨ç¾ã®å¦ç¿ã¯åãã¯é£ããæããããããã¾ããããç¿å¾ããã°ããã¹ãå¦çã®åé¡ã«å¯¾ãã¦é常ã«å¼·åãªãã¼ã«ãæã«å ¥ãããã¨ã«ãªãã¾ãããã®çµæãããè¤éãªåé¡ã«ã対å¿ã§ããããã«ãªããããã°ã©ãã³ã°ã®å¹ ãåºããã§ãããã
æ£è¦è¡¨ç¾ã®ä¸è¦§ãæãã¦ããã ãã¾ãããï¼
User
æ£è¦è¡¨ç¾ã®ä¸è¦§ãæãã¦ããã ãã¾ãããï¼
ChatGPT
æ£è¦è¡¨ç¾ã§ãã使ç¨ãããæ§æãç¹æ®æåã®ä¸è¦§ã以ä¸ã«ç¤ºãã¾ãããããã¯å¤ãã®ããã°ã©ãã³ã°è¨èªããã¼ã«ã§å
±éãã¦å©ç¨ããã¦ãã¾ãã
âç¹æ®æå
.ï¼ãããï¼: ä»»æã®åä¸ã®æåã«ããããã¾ãï¼æ¹è¡æåãé¤ãå ´åãå¤ãï¼ã
^: æååã®éå§ã«ããããã¾ãã
$: æååã®çµäºã«ããããã¾ãã
*: ç´åã®æåã®0å以ä¸ã®ç¹°ãè¿ãã«ããããã¾ãã
+: ç´åã®æåã®1å以ä¸ã®ç¹°ãè¿ãã«ããããã¾ãã
?: ç´åã®æåã®0åã¾ãã¯1åã®åºç¾ã«ããããã¾ãã
\: 次ã«æ¥ãç¹æ®æåãã¨ã¹ã±ã¼ãããï¼ç¹æ®ãªæå³ãç¡å¹åããï¼ããã«ä½¿ç¨ãã¾ãã
|: ããããã®ãã¿ã¼ã³ã«ããããããã¨ã表ãã¾ãï¼è«çåï¼ã
âãã£ã©ã¯ã¿ã¯ã©ã¹
[abc]: æ¬å¼§å
ã®ä»»æã®åä¸ã®æåï¼ãã®ä¾ã§ã¯ a, b, cï¼ã«ããããã¾ãã
[^abc]: æ¬å¼§å ã«ãªãä»»æã®åä¸ã®æåã«ããããã¾ãã
[a-z]: æå®ãããç¯å²å ã®ä»»æã®åä¸ã®æåã«ããããã¾ãï¼ãã®ä¾ã§ã¯å°æåã®ã¢ã«ãã¡ãããï¼ã
[A-Z]: æå®ãããç¯å²å ã®ä»»æã®åä¸ã®å¤§æåã¢ã«ãã¡ãããã«ããããã¾ãã
[0-9]: ä»»æã®åä¸ã®æ°åã«ããããã¾ãã
âããªã»ãããã£ã©ã¯ã¿ã¯ã©ã¹
\d: ä»»æã®æ°åã«ããããã¾ãï¼[0-9]ã¨åçï¼ã
\D: æ°å以å¤ã®ä»»æã®æåã«ããããã¾ãï¼[^0-9]ã¨åçï¼ã
\w: è±æ°åã¾ãã¯ã¢ã³ãã¼ã¹ã³ã¢ï¼ï¼ã«ããããã¾ãï¼[a-zA-Z0-9]ã¨åçï¼ã
\W: \wã«ãããããªãæåã«ããããã¾ãã
\s: ä»»æã®ç©ºç½æåï¼ã¹ãã¼ã¹ãã¿ããæ¹è¡ãªã©ï¼ã«ããããã¾ãã
\S: 空ç½æå以å¤ã®ä»»æã®æåã«ããããã¾ãã
âéæå®å
{n}: ç´åã®æåã®æ£ç¢ºã«nåã®ç¹°ãè¿ãã«ããããã¾ãã
{n,}: ç´åã®æåã®nå以ä¸ã®ç¹°ãè¿ãã«ããããã¾ãã
{n,m}: ç´åã®æåã®nå以ä¸ãmå以ä¸ã®ç¹°ãè¿ãã«ããããã¾ãã
ãããã®è¦ç´ ãçµã¿åããã¦ãé常ã«è¤éãªããã¹ããã¿ã¼ã³ã表ç¾ãããã¨ãã§ãã¾ããæ£è¦è¡¨ç¾ãå¹æçã«ä½¿ãããªããã¨ã§ãããã¹ãå¦çã®å¹çãå¤§å¹ ã«åä¸ãã¾ãã
ããããã«ã¤ãã¦ãå ·ä½ä¾ããé¡ããã¾ããRubyã®ã³ã¼ãã§ãé¡ããã¾ãã
User
ãããã¨ããããã¾ãã
ããããã«ã¤ãã¦ãå ·ä½ä¾ããé¡ããã¾ããRubyã®ã³ã¼ãã§ãé¡ããã¾ãã
ChatGPT
Rubyã§æ£è¦è¡¨ç¾ã使ç¨ããä¾ãããããã®æ§æã«ã¤ãã¦ç¤ºãã¾ããRubyã§ã¯æ£è¦è¡¨ç¾ã/.../ã§å²ããã¨ã§è¡¨ç¾ãã¾ãã
âç¹æ®æå
.ï¼ãããï¼
"cat" =~ /c.t/ # "cat"ã®"c"ã¨"t"ã®éã«ä»»æã®1æåãããå ´åã«ããã
^
"hello" =~ /^h/ # "hello"ã"h"ã§å§ã¾ãå ´åã«ããã
$
"end" =~ /d$/ # "end"ã"d"ã§çµããå ´åã«ããã
*
"baaaaat" =~ /ba*t/ # "b"ã®å¾ã«"a"ã0å以ä¸ç¶ãã"t"ãæ¥ãå ´åã«ããã
+
"baaaaat" =~ /ba+t/ # "b"ã®å¾ã«"a"ã1å以ä¸ç¶ãã"t"ãæ¥ãå ´åã«ããã
?
"bat" =~ /ba?t/ # "b"ã®å¾ã«"a"ã0åã¾ãã¯1åç¶ãã"t"ãæ¥ãå ´åã«ããã
\
"3.14" =~ /3\.14/ # "3.14"ã®æååã«ããªãªããå«ãã¦æ£ç¢ºã«ããã
|
"bat" =~ /b(a|e)t/ # "bat"ã¾ãã¯"bet"ã«ããã
âãã£ã©ã¯ã¿ã¯ã©ã¹
[abc]
"a" =~ /[abc]/ # "a", "b", "c"ã®ããããã«ããã
[^abc]
"d" =~ /[^abc]/ # "a", "b", "c"以å¤ã®æåã«ããã
[a-z]
"m" =~ /[a-z]/ # å°æåã®ã¢ã«ãã¡ãããã«ããã
[A-Z]
"M" =~ /[A-Z]/ # 大æåã®ã¢ã«ãã¡ãããã«ããã
[0-9]
"9" =~ /[0-9]/ # æ°åã«ããã
âããªã»ãããã£ã©ã¯ã¿ã¯ã©ã¹
\d
"7" =~ /\d/ # æ°åã«ããã
\D
"x" =~ /\D/ # æ°å以å¤ã®æåã«ããã
\w
"_" =~ /\w/ # è±æ°åã¾ãã¯ã¢ã³ãã¼ã¹ã³ã¢ã«ããã
\W
"!" =~ /\W/ # è±æ°åãã¢ã³ãã¼ã¹ã³ã¢ä»¥å¤ã®æåã«ããã
\s
" " =~ /\s/ # 空ç½æåã«ããã
\S
"X" =~ /\S/ # 空ç½æå以å¤ã®ä»»æã®æåã«ããã
âéæå®å
{n}
"aaaa" =~ /a{4}/ # "a"ãæ£ç¢ºã«4åç¶ãå ´åã«ããã
{n,}
"aaaaa" =~ /a{2,}/ # "a"ã2å以ä¸ç¶ãå ´åã«ããã
{n,m}
"aa" =~ /a{1,3}/ # "a"ã1å以ä¸3å以ä¸ã§ç¶ãå ´åã«ããã
ãããã®ä¾ã¯Rubyã®æ£è¦è¡¨ç¾ãæ´»ç¨ãã¦æ§ã ãªããã¹ããã¿ã¼ã³ãããããããåºæ¬çãªæ¹æ³ã示ãã¦ãã¾ããããã«ãããããã¹ãå¦çã®å¤ãã®èª²é¡ã«å¯¾å¿ã§ãã¾ãã
ãããã¨ããããã¾ãããã¹ã¦ãå¼ãtrueãè¿ãå ´åã«ãªãã¾ããã
User
ãããã¨ããããã¾ãããã¹ã¦ãå¼ãtrueãè¿ãå ´åã«ãªãã¾ããã
ChatGPT
ã¯ãããã®éãã§ããåä¾ã¯ãæå®ããããã¿ã¼ã³ã«æ£ç¢ºã«ãããããå ´åãRubyã®æ£è¦è¡¨ç¾ãããæ¼ç®å=~ã0以ä¸ã®å¤ï¼ãããããä½ç½®ã®ã¤ã³ããã¯ã¹ï¼ãè¿ããããã¯è«çå¤ã¨ãã¦è©ä¾¡ãããå ´åã«trueã¨ãªãã¾ãããããããªãå ´åã¯nilãè¿ããããã¯è«çå¤ã¨ãã¦falseã¨è©ä¾¡ããã¾ãã
ãããã®ä¾ãæ´»ç¨ãã¦ãæ§ã ãªããã¹ãå¦çã®ã·ããªãªã«å¿ç¨ãããã¨ãã§ãã¾ããæ£è¦è¡¨ç¾ã¯é常ã«å¼·åã§ãå¦ã¶ä¾¡å¤ã®ãããã¼ã«ã§ããä½ãä»ã«è³ªåãããã°ãã©ãããç¥ãããã ããï¼
æ£è¦è¡¨ç¾ã®ãå èªã¿ããå¾èªã¿ãã®è¡¨ç¾ã«ã¤ãã¦ãæãã¦ãã ããã
User
ããå°ããä»ãåããã ããã
æ£è¦è¡¨ç¾ã®ãå èªã¿ããå¾èªã¿ãã®è¡¨ç¾ã«ã¤ãã¦ãæãã¦ãã ããã
ChatGPT
æ£è¦è¡¨ç¾ã®ãå
èªã¿ãã¨ãå¾èªã¿ãã¯ãç¹å®ã®ãã¿ã¼ã³ã«å¯¾ãã¦ããã®åå¾ã®ããã¹ããæ¡ä»¶ã¨ãã¦æ¤è¨¼ãããã¨ãã§ããé常ã«ä¾¿å©ãªæ©è½ã§ãããããã¯ããã¹ããæ¶è²»ããªãï¼ãããããé¨åãçµæã«å«ã¾ãªãï¼ã¢ãµã¼ã·ã§ã³ã¨ãã¦åä½ãã¾ããRubyã§ããããã®è¡¨ç¾ãå©ç¨ãããã¨ãã§ãã¾ãã
âå
èªã¿ï¼Lookaheadï¼
å
èªã¿ã¢ãµã¼ã·ã§ã³ã«ã¯ãè¯å®çå
èªã¿ãã¨ãå¦å®çå
èªã¿ãã®äºç¨®é¡ãããã¾ãã
è¯å®çå
èªã¿ (?=...)
ãããã¿ã¼ã³ã®ç´å¾ã«ç¹å®ã®ãã¿ã¼ã³ãç¶ãå ´åã«ããããã¾ããããã®å¾ãã®ãã¿ã¼ã³ã¯ãããçµæã«ã¯å«ã¾ãã¾ããã
"window7" =~ /win(?=dow)/ # "win"ã®å¾ã«"dow"ãç¶ãå ´åã«ãããããããé¨åã¯"win"ã®ã¿ã
å¦å®çå
èªã¿ (?!...)
ãããã¿ã¼ã³ã®ç´å¾ã«ç¹å®ã®ãã¿ã¼ã³ãç¶ããªãå ´åã«ããããã¾ãããå¾ãã®ãã¿ã¼ã³ã¯æ¤è¨¼ã®ã¿ã«ä½¿ç¨ããããããçµæã«ã¯å«ã¾ãã¾ããã
"win7" =~ /win(?!dow)/ # "win"ã®å¾ã«"dow"ãç¶ããªãå ´åã«ãããããããé¨åã¯"win"ã
âå¾èªã¿ï¼Lookbehindï¼
å¾èªã¿ã¢ãµã¼ã·ã§ã³ã«ããè¯å®çå¾èªã¿ãã¨ãå¦å®çå¾èªã¿ããããã¾ãã
è¯å®çå¾èªã¿ (?<=...)
ç¹å®ã®ãã¿ã¼ã³ããããã¿ã¼ã³ã®ç´åã«åå¨ããå ´åã«ããããã¾ãããåã®ãã¿ã¼ã³ã¯ãããçµæã«ã¯å«ã¾ãã¾ããã
"7window" =~ /(?<=7)window/ # "window"ã®åã«"7"ãããå ´åã«ãããããããé¨åã¯"window"ã
å¦å®çå¾èªã¿ (?<!...)
ç¹å®ã®ãã¿ã¼ã³ããããã¿ã¼ã³ã®ç´åã«åå¨ããªãå ´åã«ããããã¾ãããåã®ãã¿ã¼ã³ã¯æ¤è¨¼ã®ã¿ã«ä½¿ç¨ããããããçµæã«ã¯å«ã¾ãã¾ããã
"window" =~ /(?<!7)window/ # "window"ã®åã«"7"ããªãå ´åã«ãããããããé¨åã¯"window"ã
ãããã®å èªã¿ã¨å¾èªã¿ã¢ãµã¼ã·ã§ã³ã使ããã¨ã§ããããããããæååã®åå¾ã®æ¡ä»¶ãé常ã«æè»ã«æå®ã§ããããã«ãªãã¾ããããã«ãããããè¤éãªããã¹ããã¿ã¼ã³ãå¹ççã«å¦çãããã¨ãå¯è½ã§ãã
å èªã¿ãå¾èªã¿ã¯ãåæã®æ£è¦è¡¨ç¾ã®å®è£ ã«ãå«ã¾ãã¦ããã®ã§ããï¼
User
å
èªã¿ãå¾èªã¿ã¯ãåæã®æ£è¦è¡¨ç¾ã®å®è£
ã«ãå«ã¾ãã¦ããã®ã§ããï¼
ChatGPT
å
èªã¿ã¨å¾èªã¿ï¼ä¸è¬ã«ãå
èªã¿ã»å¾èªã¿ã¢ãµã¼ã·ã§ã³ãã¨ãã¦ç¥ãããï¼ã¯ãåæã®æ£è¦è¡¨ç¾ã®å®è£
ã«ã¯å«ã¾ãã¦ãã¾ããã§ããããããã®æ©è½ã¯ããç¾ä»£çãªæ£è¦è¡¨ç¾ã®æ¡å¼µã¨ãã¦å¾ã«å°å
¥ããã¾ããã
æ£è¦è¡¨ç¾ã®æ¦å¿µèªä½ã¯1950年代ã«ã¹ãã£ã¼ãã³ã»ã¯ãªã¼ãã«ãã£ã¦æå±ããã¾ããããå½æã®è¡¨ç¾ã¯ç¾å¨ã®ãããªè¤éãªããã¹ãå¦çè½åãæã£ã¦ãã¾ããã§ãããåæã®å®è£ ã§ã¯ãåºæ¬çãªæåã®ãããã³ã°ãç¹°ãè¿ããããã³é¸æãªã©ãå¯è½ã§ããããããã¹ãã®ãã¿ã¼ã³ãæ¶è²»ããã«ãã§ãã¯ããå èªã¿ãå¾èªã¿ã®ãããªæ´ç·´ãããæ©è½ã¯å«ã¾ãã¦ãã¾ããã§ããã
å èªã¿ã¢ãµã¼ã·ã§ã³ã¯1990年代ã«Perlè¨èªã«å°å ¥ããããã¨ã§åºã¾ãããã®å¾å¤ãã®ããã°ã©ãã³ã°è¨èªã®æ£è¦è¡¨ç¾ã©ã¤ãã©ãªã«æ¡ç¨ããã¾ãããå¾èªã¿ã¢ãµã¼ã·ã§ã³ãåæ§ã«ãPerlãªã©ã®è¨èªãéãã¦æ®åãã¾ããããããã®æ©è½ã¯ãããè¤éãªæ¡ä»¶ã¨ã³ã³ããã¹ãã«ä¾åãããããã³ã°ãè¦æ±ãããå ´é¢ã§é常ã«æç¨ã§ãã
ãããã£ã¦ãå èªã¿ã¨å¾èªã¿ã¯æ£è¦è¡¨ç¾ã®æ©è½ãé²åããæ¡å¼µãããä¸ã§å¾ãã追å ãããæ©è½ã§ããããã®è¿½å ã«ãã£ã¦æ£è¦è¡¨ç¾ã®è¡¨ç¾åã¨å©ç¨ã®å¹ ã大ããåºããã¾ããã
æååã®ç½®æã«ã¤ãã¦ãæãã¦ãã ããã
User
æååã®ç½®æã«ã¤ãã¦ãæãã¦ãã ããã
ChatGPT
æååã®ç½®æã¯ãæ£è¦è¡¨ç¾ã使ããã¨ã§ç¹å®ã®ãã¿ã¼ã³ã«ãããããé¨åãå¥ã®æååã§ç½®ãæããå¦çãæãã¾ããããã¯ããã¹ãç·¨éããã¼ã¿ã®æ´å½¢ãããã°ã©ãã³ã°ã«ããã¦é常ã«ãã使ãããæ©è½ã§ãã
Rubyã«ãããæååã®ç½®ææ¹æ³
Rubyã§ã¯ãgsubã¡ã½ããã使ã£ã¦æååå
ã®ãã¿ã¼ã³ãç½®æãã¾ãããã®ã¡ã½ããã¯ã対象ã®æååã«å«ã¾ãããã¹ã¦ã®ãã¿ã¼ã³ã«ãããããé¨åãç½®æãã¾ããgsubã®åºæ¬çãªä½¿ãæ¹ã以ä¸ã«ç¤ºãã¾ãã
åºæ¬çãªç½®æ
text = "Hello, world!" new_text = text.gsub(/world/, "Ruby") puts new_text # "Hello, Ruby!"
ãã®ä¾ã§ã¯ã"world"ã¨ããæååã"Ruby"ã«ç½®æãã¦ãã¾ãã
æ£è¦è¡¨ç¾ã使ç¨ããè¤éãªç½®æ
ãã¿ã¼ã³ã«ã°ã«ã¼ãã使ããã¨ã§ããããããé¨åã®ä¸é¨ãåå©ç¨ããªããç½®æãè¡ããã¨ãã§ãã¾ãã
text = "Hello, world 2021!" new_text = text.gsub(/(\w+), (\w+) (\d+)/, '\2, \1 \3') puts new_text # "world, Hello 2021!"
ãã®ä¾ã§ã¯ã3ã¤ã®ãã£ããã£ã°ã«ã¼ãã使ã£ã¦ãã¾ãã(\w+)ã¯åèªã«ããããã(\d+)ã¯æ°åã«ããããã¾ããç½®æé¨åã§ã¯ã\2, \1 \3ã¨æå®ãããã¨ã§ã2çªç®ã1çªç®ã3çªç®ã®ãã£ããã£ã°ã«ã¼ãã«ãããããé¨åãæ°ããé åºã§é ç½®ãã¦ãã¾ãã
æ¡ä»¶ã«ããåçãªç½®æ
ãããã¯ã使ç¨ãã¦ããããããé¨åã«åºã¥ãã¦åçã«ç½®æããã¹ãã決å®ãããã¨ãå¯è½ã§ãã
text = "I have 15 apples and 12 oranges." new_text = text.gsub(/\d+/) { |n| n.to_i * 2 } puts new_text # "I have 30 apples and 24 oranges."
ãã®ä¾ã§ã¯ãæ°åã«ãããããé¨åãè¦ã¤ãããã®æ°åãæ´æ°ã«å¤æãã2åã«ãã¦è¿ãã¦ãã¾ããããã«ãããããã¹ãå ã®ãã¹ã¦ã®æ°å¤ã2åãããæ°ããæååãçæããã¾ãã
æååã®ç½®æãç¨ãããã¨ã§ãããã¹ããã¼ã¿ã®åãã©ã¼ãããããã¼ã¿ã®ã¯ã¬ã³ã¸ã³ã°ãç¹å®ã®æ å ±ã®æ½åºãå¤æãªã©ãå¤å²ã«ãããå¦çãå¯è½ã«ãªãã¾ãã
æ£è¦è¡¨ç¾ã§é¿ããã¹ããã¨ãªã©ãããã°æãã¦ãã ããã
User
ãããã¨ããããã¾ããã
ãã¨ãæ£è¦è¡¨ç¾ã§é¿ããã¹ããã¨ãªã©ãããã°æãã¦ãã ããã
ChatGPT
æ£è¦è¡¨ç¾ã¯é常ã«å¼·åãªãã¼ã«ã§ãããé©åã«ä½¿ç¨ããªãã¨ããã©ã¼ãã³ã¹ã®åé¡ãäºæããªãåä½ãå¼ãèµ·ããå¯è½æ§ãããã¾ãã以ä¸ã«æ£è¦è¡¨ç¾ã使ç¨ããéã«é¿ããã¹ãããã¤ãã®ãã¤ã³ããæãã¾ãã
1. éå°ãªããã¯ãã©ããã³ã°
è¤éãªæ£è¦è¡¨ç¾ãç¹ã«å¤ãã®éæå®åï¼*ã+ã?ã{n,m}ï¼ã使ç¨ããã¨ãã¨ã³ã¸ã³ãå¤ãã®ããã¯ãã©ããã³ã°ãè¡ããã¨ã«ãªãããããããã©ã¼ãã³ã¹ã®ä½ä¸ãæããã¨ãããã¾ããç¹ã«ã"catastrophic backtracking"ã¨å¼ã°ããç¾è±¡ã«æ³¨æãå¿
è¦ã§ãããã¯æ£è¦è¡¨ç¾ãå
¥åæååã«å¯¾ãã¦æ¥µç«¯ã«å¤ãã®æéãè¦ããåå ã¨ãªãã¾ãã
2. ãã£ããã£ã°ã«ã¼ãã®é度ã®ä½¿ç¨
ãã£ããã£ã°ã«ã¼ãï¼()ã§å²ã¾ããé¨åï¼ã¯ä¾¿å©ã§ããã使ãéããã¨æ£è¦è¡¨ç¾ã®å¦çé度ãé
ããªããã¨ãããã¾ããç¹ã«ããã£ããã£ãå¿
è¦ãªãå ´åã¯éãã£ããã£ã°ã«ã¼ãï¼(?:...)ï¼ã使ç¨ããã¨è¯ãã§ãããã
3. æ£è¦è¡¨ç¾ã®é度ãªè¤éå
é常ã«é·ãè¤éãªæ£è¦è¡¨ç¾ã¯ãèªã¿ã«ãããä¿å®ãå°é£ã«ãªãã¾ããå¯è½ãªéãã·ã³ãã«ã§ãç®çã«åã£ãæ£è¦è¡¨ç¾ã使ç¨ãããã¨ãæ¨å¥¨ããã¾ããã¾ããæ£è¦è¡¨ç¾ãè¤æ°ã«åãã¦ãåé¨åãæ確ã«ãããã¨ãæå¹ã§ãã
4. ã»ãã¥ãªãã£ãªã¹ã¯ã®ç¡è¦
ã¦ã¼ã¶ã¼å
¥åãæ£è¦è¡¨ç¾ã§å¦çããå ´åãé©åãªã¨ã¹ã±ã¼ããæ¤è¨¼ãè¡ããªãã¨ãã»ãã¥ãªãã£ä¸ã®åé¡ãçºçããå¯è½æ§ãããã¾ããä¾ãã°ãç¹å®ã®æ£è¦è¡¨ç¾ãæªç¨ãã¦ãµã¼ãã¹æå¦æ»æï¼DoSæ»æï¼ãå¼ãèµ·ãããã¨ãã§ãã¾ãã
5. æ£è¦è¡¨ç¾ã«ä¾åãããã
æ£è¦è¡¨ç¾ã¯å¤ãã®åé¡ã«å¯¾ãã¦å¼·åãªã½ãªã¥ã¼ã·ã§ã³ãæä¾ãã¾ããããã¹ã¦ã®æååå¦çãæ£è¦è¡¨ç¾ã«é©ãã¦ããããã§ã¯ããã¾ãããä¾ãã°ãé常ã«åç´ãªæååæä½ã¯ãæ£è¦è¡¨ç¾ã使ããã«æååé¢æ°ã§å¦çããæ¹ãå¹ççãªå ´åãããã¾ãã
æ£è¦è¡¨ç¾ã使ãéã¯ããããã®ç¹ã«æ³¨æããé©åãªã±ã¼ã¹ã§ã®ã¿ä½¿ç¨ããããã«å¿ããããã¨ã大åã§ããããã«ãããã³ã¼ãã®å¹çã¨å¯èªæ§ãä¿ã¡ãªãããæ£è¦è¡¨ç¾ã®ã¡ãªãããæ大éã«æ´»ç¨ãããã¨ãã§ãã¾ãã
éãã£ããã£ã°ã«ã¼ãã®ä½¿ãæ¹ãæãã¦ãã ããã
User
éãã£ããã£ã°ã«ã¼ãã®ä½¿ãæ¹ãæãã¦ãã ããã
ChatGPT
éãã£ããã£ã°ã«ã¼ãã¯ãæ£è¦è¡¨ç¾ã®ä¸é¨ãã°ã«ã¼ãåãããã¨ã§ããã®é¨åã«å¯¾ããæä½ï¼ç¹°ãè¿ããé¸æãæ¡ä»¶ä»ããããã³ã°ãªã©ï¼ãå¹ççã«è¡ããã¨ãã§ãã¾ããããããçµæããã¯é¤å¤ããã¾ããããã¯ããããã³ã°ããã»ã¹ã§ãã®ã°ã«ã¼ããè¨æ¶ããå¿
è¦ããªããããããã©ã¼ãã³ã¹ãåä¸ãããå ´åãããã¾ãã
éãã£ããã£ã°ã«ã¼ãã®æ§æ
éãã£ããã£ã°ã«ã¼ãã使ç¨ããã«ã¯ãã°ã«ã¼ãã®éå§ã示ãæ¬å¼§ã®ç´å¾ã«?:ãç½®ãã¾ããåºæ¬çãªæ§æã¯æ¬¡ã®ããã«ãªãã¾ãã
(?:ãã¿ã¼ã³)
éãã£ããã£ã°ã«ã¼ãã®ä½¿ç¨ä¾
åºæ¬çãªä¾
text = "the quick brown fox" # "quick"ã¾ãã¯"brown"ã®å¾ã«" fox"ãç¶ãå ´åã«ããããããã"quick"ã"brown"ã¯ãã£ããã£ããªã matches = text.scan(/(?:quick|brown) fox/) puts matches.inspect # ["quick fox", "brown fox"]
ãã®ä¾ã§ã¯ãquickã¾ãã¯brownã«ããããã¾ããããããã®åèªèªä½ã¯çµæã¨ãã¦è¿ããã¾ãããscanã¡ã½ããã¯ãããããå ¨ä½ã®é¨åãè¿ãã¾ããããã£ããã£ã°ã«ã¼ãããªããããããã®åèªã¯åå¥ã«ã¯è¿ããã¾ããã
è¤éãªæ¡ä»¶ã§ã®ä½¿ç¨
text = "John: 1234, Jane: 5678, Jake: 9012" # ååã®å¾ã«ç¶ãæ°åãæ¤ç´¢ããããååã¯ãã£ããã£ããªã numbers = text.scan(/(?:[A-Za-z]+: )(\d+)/) puts numbers.inspect # [["1234"], ["5678"], ["9012"]]
ãã®ä¾ã§ã¯ãåååã®å¾ã«ã³ãã³ã¨ã¹ãã¼ã¹ãç¶ãããã®å¾ã«æ°åãç¶ããã¿ã¼ã³ãæ¢ãã¦ãã¾ããååèªä½ã¯éãã£ããã£ã°ã«ã¼ã(?:[A-Za-z]+: )ã使ç¨ãã¦ãã£ããã£ããé¤å¤ãã¦ãã¾ãããæ°åã¯ãã£ããã£ã°ã«ã¼ã(\d+)ã§ãã£ããã£ãã¦ãã¾ãã
éãã£ããã£ã°ã«ã¼ãã®å©ç¹
ããã©ã¼ãã³ã¹: ä¸è¦ãªãã£ããã£æ
å ±ãä¿åããªããããæ£è¦è¡¨ç¾ã®å¦çãé«éã«ãªãå¯è½æ§ãããã¾ãã
å¯èªæ§: ãã£ããã£ã°ã«ã¼ãã使ãã¨ãã©ã®é¨åãéè¦ã§ä½ãçµæã¨ãã¦åå¾ãããããæ確ã«ãªãã¾ããéãã£ããã£ã°ã«ã¼ãã使ããã¨ã§ãæå³ããªããã£ããã£ããã³ã¼ããç°¡æ½ã«ä¿ã¤ãã¨ãã§ãã¾ãã
éãã£ããã£ã°ã«ã¼ããå¹æçã«ä½¿ç¨ãããã¨ã§ãæ£è¦è¡¨ç¾ã®ã¯ãªã¢ãªæ§é ãç¶æãã¤ã¤ãå¿ è¦ãªæ å ±ã ããæ½åºãããã¨ãã§ãã¾ãã