ºÇ½ª¹¹¿·¡§
orika_ex_miyako 2024ǯ01·î01Æü(·î) 13:42:28ÍúÎò
¡¡º£²ó¤Î¾ì¹ç¡¢¡ÖÀ°¿ô¤Î¤ß¤ò½ü³°¡×¤·¤¿Ê¸»úÎó¤òºîÀ®¤·¤Þ¤¹¡£
¼ç¤Ë¥Æ¥¥¹¥È½èÍý¤äñ¸ì¤ÎÃê½Ð¤¬É¬ÍפʾìÌ̤ǻÈÍѤǤ¤Þ¤¹¡£Î㤨¤Ð¡¢¿ô¼°¤äʸ¾Ï¤«¤é¥¡¼¥ï¡¼¥É¤òÃê½Ð¤¹¤ë¤Ê¤É¤ÎÍÑÅÓ¤¬¹Í¤¨¤é¤ì¤Þ¤¹¡£
using System.Collections.Generic; using System.Text.RegularExpressions; /// <summary> /// ¥Æ¥¥¹¥È¤«¤éñ¸ì¤òÃê½Ð¤¹¤ë¥æ¡¼¥Æ¥£¥ê¥Æ¥£¥¯¥é¥¹¤Ç¤¹¡£ /// </summary> public static class WordExtractor { // ñ¸ì¤òÃê½Ð¤¹¤ëÀµµ¬É½¸½¥Ñ¥¿¡¼¥ó private static readonly string WordPattern = @"\b\w+\b"; /// <summary> /// ÆþÎϥƥ¥¹¥È¤«¤éÀ°¿ô°Ê³°¤Îñ¸ì¤òÃê½Ð¤·¤Þ¤¹¡£ /// </summary> /// <param name="inputText">ñ¸ì¤òÃê½Ð¤¹¤ëÂоݤΥƥ¥¹¥È</param> /// <returns>À°¿ô°Ê³°¤Îñ¸ì¤Î¥ê¥¹¥È</returns> public static List<string> ExtractWords(string inputText) { string equationString = inputText; // Àµµ¬É½¸½¤ò»ÈÍѤ·¤Æñ¸ì¤òÃê½Ð¤¹¤ë MatchCollection matches = Regex.Matches(equationString, WordPattern); // ñ¸ì¤ò³ÊǼ¤¹¤ë¥ê¥¹¥È List<string> wordList = new (); // ¥Þ¥Ã¥Á¤·¤¿Ã±¸ì¤ò¥ê¥¹¥È¤ËÄɲà foreach (Match match in matches) { string word = match.Value; ¡¡¡¡¡¡¡¡¡¡¡¡// out ¤Ç¼èÆÀ¤·¤¿ÃͤòÍøÍѤ·¤Ê¤¤¾ì¹ç¤Ë¤ÏÊÑ¿ôÀë¸À¤Ç¤Ï¤Ê¤¯¥¢¥ó¥À¡¼¥Ð¡¼(_)¤òÍøÍѤ¹¤ë¤³¤È¤ÇÌÀ¼¨Åª¤ËÍøÍѤ·¤Ê¤¤¤³¤È¤òÅÁ¤¨¤ë¤³¤È¤¬¤Ç¤¤ë if (int.TryParse(word, out int _)) continue; wordList.Add(word); } return wordList; } }
¡¡Í¿¤¨¤é¤ì¤¿¥Æ¥¥¹¥È¤«¤éñ¸ì¤òÃê½Ð¤¹¤ë¤¿¤á¤Î¥æ¡¼¥Æ¥£¥ê¥Æ¥£¥¯¥é¥¹¤Ç¤¹¡£
¶ñÂÎŪ¤Ë¤Ï¡¢Àµµ¬É½¸½¤ò»ÈÍѤ·¤Æñ¸ì¤òÃê½Ð¤·¡¢¤½¤ÎÃ椫¤éÀ°¿ô¤È¤·¤Æ²ò¼á²Äǽ¤Ê¤â¤Î¤ò½ü³°¤·¤Æñ¸ì¤Î¥ê¥¹¥È¤òºîÀ®¤·¤Þ¤¹¡£
¡¡ExtractWords¥á¥½¥Ã¥É¤Ï¡¢¥Æ¥¥¹¥È¡ÊinputText°ú¿ô¡Ë¤ò¼õ¤±¼è¤ê¤Þ¤¹¡£
Àµµ¬É½¸½¥Ñ¥¿¡¼¥ó @"\b\w+\b" ¤ò»ÈÍѤ·¤Æ¡¢¥Æ¥¥¹¥ÈÆâ¤Îñ¸ì¤ò¸¡º÷¤·¤Þ¤¹¡£
¡¡¥Þ¥Ã¥Á¤·¤¿Ã±¸ì¤ò¥ê¥¹¥È¤ËÄɲä·¤Þ¤¹¤¬¡¢À°¿ô¤È¤·¤Æ²ò¼á²Äǽ¤Êñ¸ì¤Ï½ü³°¤·¤Þ¤¹¡£
·ë²Ì¤È¤·¤Æ¡¢À°¿ô°Ê³°¤Îñ¸ì¤Î¥ê¥¹¥È¤¬ÊÖ¤µ¤ì¤Þ¤¹¡£
¡¡¤³¤Î¥³¡¼¥É¤Îή¤ìÁ´ÂΤȤ·¤Æ¤Ï¡¢Í¿¤¨¤é¤ì¤¿¥Æ¥¥¹¥È¤«¤éÀµµ¬É½¸½¤Ë°ìÃפ¹¤ëñ¸ì¤òÃê½Ð¤·¡¢
¤½¤ÎÃ椫¤éÀ°¿ô¤È¤·¤Æ²ò¼á²Äǽ¤Ê¤â¤Î¤ò½ü³°¤·¤¿Ê¸»úÎó¤Î¥ê¥¹¥È¤òºîÀ®¤·¤Æ¤¤¤ë¡¢¤È¤¤¤¦¤â¤Î¤Ç¤¹¡£
¡¡º£²óÍøÍѤ·¤Æ¤¤¤ë MatchCollection ¥¯¥é¥¹¤Ï System.Text.RegularExpressions ¤Ë´Þ¤Þ¤ì¤ë¤â¤Î¤Ç¤¹¡£
¸½»þÅÀ(2024ǯ1·î)¤Ç MatchCollection ¥¯¥é¥¹¤Ï IEnumerable<T> ¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤ò¼ÂÁõ¤·¤Æ¤¤¤ë¤¿¤á¡¢LINQ ¤ò³èÍѤǤ¤Þ¤¹¡£
MicroSoft
System.Text.RegularExpressions.MatchCollection ¥¯¥é¥¹
https://learn.microsoft.com/ja-jp/dotnet/api/syste...
¡¡LINQ ¤ò³èÍѤ·¤Æ¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°¤·¤Þ¤·¤ç¤¦¡£
using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; /// <summary> /// ¥Æ¥¥¹¥È¤«¤éñ¸ì¤òÃê½Ð¤¹¤ë¥æ¡¼¥Æ¥£¥ê¥Æ¥£¥¯¥é¥¹¤Ç¤¹¡£ /// </summary> public static class WordExtractor { // ñ¸ì¤òÃê½Ð¤¹¤ëÀµµ¬É½¸½¥Ñ¥¿¡¼¥ó private static readonly string WordPattern = @"\b\w+\b"; /// <summary> /// ÆþÎϥƥ¥¹¥È¤«¤éÀ°¿ô°Ê³°¤Îñ¸ì¤òÃê½Ð¤·¤Þ¤¹¡£ /// </summary> /// <param name="inputText">ñ¸ì¤òÃê½Ð¤¹¤ëÂоݤΥƥ¥¹¥È</param> /// <returns>À°¿ô°Ê³°¤Îñ¸ì¤Î¥ê¥¹¥È</returns> public static List<string> ExtractWords(string inputText) { // Àµµ¬É½¸½¤ò»ÈÍѤ·¤Æñ¸ì¤òÃê½Ð¤·¡¢À°¿ô°Ê³°¤Îñ¸ì¤ò¥Õ¥£¥ë¥¿¥ê¥ó¥° MatchCollection matches = Regex.Matches(inputText, WordPattern); // MatchCollection ¤Ï IEnumerable<Match> ¤È¤·¤Æ°·¤¨¤ë var words = matches .Cast<Match>() // MatchCollection ¤«¤é IEnumerable<Match> ¤ËÊÑ´¹¡Ê¼ÂºÝ¤Ë¤ÏÉÔÍפǤ¹¤¬¡¢ÌÀ¼¨Åª¤Ë¼¨¤·¤Æ¤¤¤Þ¤¹¡Ë .Select(match => match.Value) .Where(word => !int.TryParse(word, out _)) .ToList(); return words; } }
¡¡Regex.Matches ¤Ï MatchCollection ¤òÊÖ¤·¤Þ¤¹¡£
¤³¤Î¥á¥½¥Ã¥É¤Ï¡¢»ØÄꤵ¤ì¤¿Àµµ¬É½¸½¥Ñ¥¿¡¼¥ó¤Ë°ìÃפ¹¤ë¥Æ¥¥¹¥ÈÆâ¤Î¤¹¤Ù¤Æ¤ÎÉôʬʸ»úÎó¤ò¸¡º÷¤·¤Þ¤¹¡£
¡¡Cast<Match>() ¤ò»ÈÍѤ·¤Æ¡¢MatchCollection ¤ò IEnumerable<Match> ¤ËÊÑ´¹¤·¤Æ¤¤¤Þ¤¹¡£
¸½ºß¤Î MatchCollection ¤Ï IEnumerable<T> ¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤ò¼ÂÁõ¤·¤Æ¤¤¤Þ¤¹¤Î¤Ç¡¢LINQ Áàºî¤¬´Êñ¤Ë¹Ô¤¨¤Þ¤¹¡£
¤½¤Î¤¿¤á¡¢¤³¤³¤Ç¤ÏÌÀ¼¨Åª¤ËµºÜ¤·¤Æ¤¤¤Þ¤¹¤¬¡¢¤³¤Î Cast ¥á¥½¥Ã¥É¤ÏÉÔÍפǤ¹¡£
¡¡Select ¤Ï LINQ ¥á¥½¥Ã¥É¤Ç¡¢³Æ Match ¥ª¥Ö¥¸¥§¥¯¥È¤«¤é Value ¥×¥í¥Ñ¥Æ¥£¤òÃê½Ð¤·¤Æ¤¤¤Þ¤¹¡£
Value ¥×¥í¥Ñ¥Æ¥£¤Ë¤Ï¡¢Àµµ¬É½¸½¤Ë¤è¤Ã¤Æ°ìÃפ·¤¿Éôʬʸ»úÎ󤬳ÊǼ¤µ¤ì¤Æ¤¤¤Þ¤¹¡£
¡¡Where ¤Ï LINQ ¥á¥½¥Ã¥É¤Ç¡¢int.TryParse ¤ò»ÈÍѤ·¤ÆÀ°¿ô¤È¤·¤Æ²ò¼á²Äǽ¤Êñ¸ì¤ò¥Õ¥£¥ë¥¿¥ê¥ó¥°¤·¤Æ¤¤¤Þ¤¹¡£
¡¡!int.TryParse(word, out _) ¤ÏÀ°¿ô¤ËÊÑ´¹¤Ç¤¤Ê¤¤¾ì¹ç¤Ë true ¤òÊÖ¤¹¾ò·ï¼°¤Ç¤¹¡£
Àµµ¬É½¸½¤Ë°ìÃפ¹¤ëñ¸ì¤Î¤¦¤Á¡¢int.TryParse¤¬ false ¤òÊÖ¤¹¡ÊÀ°¿ô¤È¤·¤Æ²ò¼á¤Ç¤¤Ê¤¤¡Ëñ¸ì¤À¤±¤¬¥ê¥¹¥È¤Ë´Þ¤Þ¤ì¡¢À°¿ô¤È¤·¤Æ²ò¼á²Äǽ¤Êñ¸ì¤Ï¥ê¥¹¥È¤Ë´Þ¤Þ¤ì¤Þ¤»¤ó¡£
Î㤨¤Ð¡¢"A" ¤ä "b" ¤Î¤è¤¦¤Êʸ»úÎó¤Ï¥ê¥¹¥È¤Ë´Þ¤Þ¤ì¤Þ¤¹¤¬¡¢"0" ¤ä "8" ¤Î¤è¤¦¤ÊÀ°¿ô¤È¤·¤Æ²ò¼á²Äǽ¤Êʸ»úÎó¤Ï¥ê¥¹¥È¤Ë´Þ¤Þ¤ì¤Þ¤»¤ó¡£
¤Ä¤Þ¤ê¡¢À°¿ô°Ê³°¤Îʸ»úÎó¤À¤±¤¬Ãê½Ð¤µ¤ì¡¢¥ê¥¹¥È¤Ë³ÊǼ¤µ¤ì¤Þ¤¹¡£
¡¡ToList ¤Ï LINQ ¥á¥½¥Ã¥É¤Ç¡¢ºÇ½ªÅª¤Ê·ë²Ì¤ò List<string> ¤ËÊÑ´¹¤·¤Æ¤¤¤Þ¤¹¡£
¤³¤ì¤Ë¤è¤ê¡¢¥á¥½¥Ã¥É¤ÎÌá¤êÃͤȤ·¤ÆÁàºî·ë²Ì¤ò³ÊǼ¤Ç¤¤Þ¤¹¡£
¡¡½èÍý¤ÎÆâÍƤ¬Íý²ò¤Ç¤¤ì¤Ð¡¢¤µ¤é¤Ë¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°²Äǽ¤Ç¤¹¡£
using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; /// <summary> /// ¥Æ¥¥¹¥È¤«¤éñ¸ì¤òÃê½Ð¤¹¤ë¥æ¡¼¥Æ¥£¥ê¥Æ¥£¥¯¥é¥¹¤Ç¤¹¡£ /// </summary> public static class WordExtractor { // ñ¸ì¤òÃê½Ð¤¹¤ëÀµµ¬É½¸½¥Ñ¥¿¡¼¥ó private static readonly string WordPattern = @"\b\w+\b"; /// <summary> /// ÆþÎϥƥ¥¹¥È¤«¤éÀ°¿ô°Ê³°¤Îñ¸ì¤òÃê½Ð¤·¤Þ¤¹¡£ /// </summary> /// <param name="inputText">ñ¸ì¤òÃê½Ð¤¹¤ëÂоݤΥƥ¥¹¥È</param> /// <returns>À°¿ô°Ê³°¤Îñ¸ì¤Î¥ê¥¹¥È</returns> public static List<string> ExtractWords(string inputText) { ¡¡¡¡¡¡¡¡// Àµµ¬É½¸½¤ò»ÈÍѤ·¤Æñ¸ì¤òÃê½Ð¤·¡¢À°¿ô°Ê³°¤Îñ¸ì¤ò¥Õ¥£¥ë¥¿¥ê¥ó¥°¤·¤Æ¥ê¥¹¥È¤ËÊÑ´¹¤·¤ÆÊÖ¤¹ return Regex.Matches(inputText, WordPattern) .Select(match => match.Value) .Where(word => !int.TryParse(word, out _)) .ToList(); } }
¡¡Regex.Matches ¥á¥½¥Ã¥É¤ÎÌá¤êÃÍ¤Ï MatchCollection ·¿¤Ç¤¹¤¬¡¢IEnumerable<T> ¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤ò¼ÂÁõ¤·¤Æ¤¤¤ë¤¿¤á¡¢Cast ¥á¥½¥Ã¥É¤ÏÉÔÍפǤ¹¡£
¤½¤Î¤Þ¤Þ LINQ ¤Î Select ¥á¥½¥Ã¥É¤ä Where ¥á¥½¥Ã¥É¤ò¼Â¹Ô¤¹¤ë¤³¤È¤¬²Äǽ¤Ç¤¹¡£
¡¡ºÇ¸å¤Ë ToList ¤·¤Æ List<string> ¤Ë¥¥ã¥¹¥È¤·¤Æ¤¤¤Þ¤¹¤Î¤Ç¡¢ÊÑ¿ô¤òºîÀ®¤·¤Ê¤¯¤Æ¤â ToList ¤ÎÌá¤êÃͤò¤½¤Î¤Þ¤Þ return ½ÐÍè¤Þ¤¹¡£
¡¡¤³¤Î¤è¤¦¤ËÊ£¿ô²ó¤Î¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°¤ò¹Ô¤¤¡¢¾¯¤·¤º¤ÄÍý²ò¤ò¿¼¤á¤Æ¤¤¤¯¤è¤¦¤Ë¤·¤Æ¤¯¤À¤µ¤¤¡£
¡¡º£²ó¤Î¼ÂÁõ¤Î¾ì¹ç¡¢Àµµ¬É½¸½¤Ë¤è¤êÀ°¿ô¤Î¤ß¤ò½ü³°¤·¤Æ¤¤¤Þ¤¹¡£
¡¡¸½ºß¤ÎÀµµ¬É½¸½¥Ñ¥¿¡¼¥ó @"\b\w+\b" ¤Ï¡¢Ã±¸ìÁ´ÂΤò¥Þ¥Ã¥Á¥ó¥°¤µ¤»¤ë¤â¤Î¤Ç¤¢¤ê¡¢
\w ¤Ïñ¸ì¹½À®Ê¸»ú¡Ê±Ñ¿ô»ú¤ª¤è¤Ó¥¢¥ó¥À¡¼¥¹¥³¥¢¡Ë¤òɽ¤·¤Æ¤¤¤Þ¤¹¡£
¤³¤ÎÀµµ¬É½¸½¥Ñ¥¿¡¼¥ó¤ÏÀ°¿ô°Ê³°¤Î¤¹¤Ù¤Æ¤Îñ¸ì¤ò¼è¤ê¹þ¤ß¤Þ¤¹¡£¤è¤Ã¤Æ¾®¿ôÅÀ¤ä³ç¸Ì¤Ê¤É¤âÀ°¿ô°Ê³°¤È¤·¤ÆÃê½Ð¤µ¤ì¤Þ¤¹¡£
¡¡¤â¤·¾®¿ôÅÀ¤ä³ç¸Ì¤ò½ü³°¤·¤¿¤¤¾ì¹ç¤Ï¡¢Àµµ¬É½¸½¥Ñ¥¿¡¼¥ó¤òÄ´À°¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£
¡¡°Ê²¼¤Ï¡¢À°¿ô¤ª¤è¤Ó¾®¿ôÅÀ¤ò´Þ¤Þ¤Ê¤¤Ã±¸ì¤òÃê½Ð¤¹¤ë¤¿¤á¤ÎÀµµ¬É½¸½¤ÎÎã¤Ç¤¹¡§
private static readonly string pattern = @"\b[^\d.]+\b";
¡¡¤³¤ÎÀµµ¬É½¸½¤Ç¤Ï¡¢\d ¤Ï¿ô»ú¤òɽ¤·¡¢. ¤Ï¾®¿ôÅÀ¤òɽ¤·¤Æ¤¤¤Þ¤¹¡£
[^\d.] ¤Ï¿ô»ú¤È¾®¿ôÅÀ°Ê³°¤Îʸ»ú¤òɽ¤·¤Þ¤¹¡£¤·¤¿¤¬¤Ã¤Æ¡¢\b[^\d.]+\b ¤Ïñ¸ìÁ´ÂΤ¬¿ô»ú¤ä¾®¿ôÅÀ¤ò´Þ¤Þ¤Ê¤¤¾ì¹ç¤Ë°ìÃפ·¤Þ¤¹¡£
- ¥«¥Æ¥´¥ê¡§
- ¿Ê³Ø/¥¹¥¯¡¼¥ë
- ¥×¥í¥°¥é¥ß¥ó¥°
¥³¥á¥ó¥È¤ò¤«¤¯