ä½ãã¨ä¾¿å©ã ãã©ä½ãã¨å¿ããã¡ãªãæ£è¦è¡¨ç¾ / Regexpãã®ã話ã
æ¸ãæ¹ãè²ã
ããããèªåã使ãæ¸ãæ¹ã ãæ¸ãã¦ãã
- åºæ¬ã®ä½¿ãæ¹
- ä»»ææåã«ããã
- ãã£ããã£
- æååã®ç½®ãæã
- æ®éã«è¡¨ç¤ºã§ããªãæåãåãã
- æ¬ä¼¼çã«åè§æåãå ¨è§æåãåãã
- ãã¿ã¼ã³ä½æã«ä¾¿å©ãªãµã¤ã
- ã¾ã¨ã
åºæ¬ã®ä½¿ãæ¹
æ£è¦è¡¨ç¾ã使ãç®çã¯ã
ãæååã®ä¸ããæå®ãããã¿ã¼ã³ã«ä¸è´ããé¨åãæ½åºããã
ãã¨ã大ä½ã ã¨æããã¡ãã£ã¨ãããããæååå
ã®æ¤ç´¢ã¿ãããªããã
ã¾ããåç´ã«æ£è¦è¡¨ç¾ãªãã¸ã§ã¯ããä½ãã«ã¯ãè¦ã¤ãããæååã // ã§å²ãã
regexp = /Str/
ããã¦ãããã"String"[regexp]ãã¨ããå½¢å¼ã§ä½¿ãã
"I am String"[/Str/] #=> "Str"
ä»»ææåã«ããã
ã¨ã«ãããªãã«ã§ãããããã¦ã»ãããã° /.*/ ã¨æ¸ãã
ããããã®ã¾ã¾ä½¿ã£ã¦ãæå³ã¯ç¡ãã®ã§ãä¸å®ã®åºéã«ãã
å
¨ã¦ã®æåã«ããããã¦ããçãªæã«ãããã¦ä½¿ãæãã
"abcdefghijk"[/a.*g/] #=> abcdefg .* ã b..f ã«ããããã¦ãã
ãã£ããã£
"[255, 128, 64]"
ãã[ ] ã®ä¸ã®æ°åãæ½åºããã¨ãã©ãããã®ï¼ãã
ããã1é ç®ãã¤åã£ããããã®ï¼ãã¨ãªãããã ãããããªå¿ è¦ã¯ãªãã
string = "[255, 128, 64]" if string[/\[(.*),(.*),(.*)\]/] r = $1.to_i #=> 255 g = $2.to_i #=> 128 b = $3.to_i #=> 64 end
æ½åºãããé¨åã ( ) ã§å²ãã
ãããããã¨ã°ãã¼ãã«å¤æ° $1, $2, $3, ...ã«ã
( )ã®ä¸ã®æååããããããé çªéãã«å
¥ã£ã¦ããã¨ããããã
ã¡ãªã¿ã«"[ ]"ã«ãããããããå ´åã¯
ãããã \[ \] ã¨ã¨ã¹ã±ã¼ããæããªããã°ãªããªãã
æ´ã«ããã£ã¨å³å¯ã«ããããããããªã以ä¸ã®ããã«æ¸ãããã§ããã
string = "[ 255 , 128 , 64 ]" if string[/\[\s*(\d*)\s*,\s*(\d*)\s*,\s*(\d*)\s*\]/] puts $1 #=> 255 puts $2 #=> 128 puts $3 #=> 64 end
空ç½ãä½è¨ã«å
¥ã£ã¦ã¦ãåããããæ°å以å¤ãå
¥ã£ã¦ããå¼¾ãã¦ãããã
ãã é常ã«ãã¡ããã¡ããã¦ä½ããããæ£è¦è¡¨ç¾ãã¯è¦ã«ããã
è¦ç´ ã2åã ã£ãã3åã ã£ãã4åã ã£ããã¨å¯å¤é·ãªãã
[ ]å
ãã¾ã¨ãã¦åã£ã¦ã.splitã§å¥åã£ã¦ãããã®ãç¡é£ããã
string = "[255, 128, 64, 32]" if string[/\[(.*)\]/] $1.split(",").each { |val| p val.to_i } #=> 255 128 64 32 end
æååã®ç½®ãæã
æ£è¦è¡¨ç¾ã§ãããããæååããã®ã¾ã¾ç½®ãæãããã¨ãã§ããã
ä¸åç½®ãæããã ããªããããªæãã
text = "12345" text[/[12345]/] = "n" puts text #=> n2345
å ¨é¨ç½®ãæãããå ´å㯠.gsub ã¨ããã¡ã½ããã使ãã°å¤§ä½ããã¨æãã
string = "12345" string.gsub!(/([135])/) { $1.to_i + 1 } puts string #=> 22446
æ®éã«è¡¨ç¤ºã§ããªãæåãåãã
ã¬ã¢ã±ã¼ã¹ããã¼ãã¼ãã®å
¥åãWin32APIã§åã£ã¦ã
ãããæåã«å¤æãã¦ããã¹ãå
¥åãã§ãããçãªãã¨ããããã¨ããæã®ã話ã
ä¸é¨ã®ãã¼ã¯å
¥åã«å¯¾ãã¦\e(ã¨ã¹ã±ã¼ã)ã¨ã\b(ããã¯ã¹ãã¼ã¹)ã¨ãã
æååã«è¿½å ãã¦ãæ®éã«è¡¨ç¤ºã§ããªãæåãéã£ã¦ããã
ããããã®ããã¾ããã£ã«ã¿ã¼ãããã¨ãåºæ¥ãªããã¨ã¨ããããè¡ãçããã®ãã³ã¬ã
/\p{Other}/
è¦æ
£ããªãæ¸ãæ¹ã ã¨æãããUnicode ããããã£ã«ããæåã¯ã©ã¹æå®ããããã
Otherã¨ããã¯ã©ã¹ãæå®ãããã¨ã§ãæ®éã«è¡¨ç¤ºã§ããªãæãã®æåãåå¾ã§ããã
\P{Other}ã¨pã大æåã§æ¸ãã°å転ãããã¤ã¾ãæ®éã«è¡¨ç¤ºã§ããæåãåããã
æ¬ä¼¼çã«åè§æåãå ¨è§æåãåãã
æåã®å¹
ãåç´è¨ç®ããããªã¼ã¨æããåè§å
¨è§ã§åããã°ããã¨ããæã«å°ã£ã¦ãã¾ã£ãã
æå¤ã¨åè§ã»å
¨è§ãæ£ç¢ºã«åããæçããªãã®ã ã
ããããã®ã¯ããUnicodeã®æåãç¹ã«æ±ã¢ã¸ã¢ã®æåï¼ã¯
ãå
¨è§åè§ã®ãã¿åããææ§ããªæåãããããããããã£ã±ãã¨åãããã¨ã¯ã§ããªã模æ§ã
ã©ããããã¨è¡ãçããå ã«è¾¿ãçããæ£è¦è¡¨ç¾ãããã ã£ãã
/[。-ï¾[:ascii:]]/
ã¾ã¼ãè¦æ
£ããªãæ£è¦è¡¨ç¾ãæã¡åºãã¦ããããã®äººã¯â¦(ç§ã )
ãã®å¤é¡ã¿ãããªæ£è¦è¡¨ç¾ã§
- ASCIIæå(åè§è±æ°å)
- åè§ã«ãæå
- åè§è¨å·(æ¥æ¬èª)
ãããæ¯ãåãã¦ãããããã ã
ã¡ãªã¿ã«ããã®ï½¡ã¨ï¾ã¯åè§ã ãããï¼å¥ç¹ã¨åæ¿ç¹ã®åè§ã ãããï¼
ãã¨æ¬å½ã«ã©ãã§ããããã©ãåè§åæ¿ç¹ã®æ¹ã¯ãã£ã¬ãããåãããã
deleteã§æ¶ãããããæã®æåããããããè¬ã
[:ascii:]ã¯POSIXæåã¯ã©ã¹ã¨ããä½æããããæåã¯ã©ã¹ã®ä¸ã§ã®ã¿ä½¿ç¨å¯è½ã
ã¡ãªã¿ã«ãã®è¨äºã大å¤åèã«ãªãã¾ããããããã¨ããããã¾ãã
ãã¿ã¼ã³ä½æã«ä¾¿å©ãªãµã¤ã
ã¾ãã¯æ®éã«å ¬å¼ã®ããã¥ã¡ã³ãã
次ã«ãRubularãã¨ããRubyã§ã®æ£è¦è¡¨ç¾ãç°¡åã«è©¦ãããµã¤ãã
è±èªã®ãµã¤ããªã®ã§ã¡ãã£ã¨ããããããèªåãã©ã£ãã§ç´¹ä»ããã¦ãããç¥ã£ãã
ãã¨æ®éã«ãªãã§ãããã§ã試ãã®ã«ä¾¿å©ãªWandboxã¨ãã
ã¾ã¨ã
æ£è¦è¡¨ç¾ã¯ãããããï¼
ã§ãã人ã®å
¥åã¨ããããããã®ã®åãæ±ãã«ã¯ã¨ã£ã¦ã便å©ã ãã
ç¥ã£ã¨ãã«è¶ãããã¨ã¯ãªããã ããï½
ï¼ã¾ã¨ãããããªãã¨ããªãã£ãï¼