{"id":3717,"date":"2012-09-11T11:02:43","date_gmt":"2012-09-10T17:02:43","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/archives\/3717"},"modified":"2012-09-18T08:27:22","modified_gmt":"2012-09-17T23:27:22","slug":"cstring%e3%81%ab%e6%ad%a3%e8%a6%8f%e8%a1%a8%e7%8f%be%e3%81%ae%e6%8b%a1%e5%bc%b5%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%82%92%e8%bf%bd%e5%8a%a0%e3%81%97%e3%81%a6linq%e3%81%a7%e4%bd%bf%e3%81%86","status":"publish","type":"post","link":"http:\/\/www.moonmile.net\/blog\/archives\/3717","title":{"rendered":"[C#]String\u306b\u6b63\u898f\u8868\u73fe\u306e\u62e1\u5f35\u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3057\u3066LINQ\u3067\u4f7f\u3046"},"content":{"rendered":"<p>System::String \u306b\u6b63\u898f\u8868\u73fe\u304c\u4f7f\u3048\u305f\u3089\u3044\u3044\u306a\u3041\u3068\u601d\u3044\u3064\u3064\u3001\u66f8\u3044\u3066\u307f\u305f\u306e\u304c\u3053\u308c\u3067\u3059\u3002<\/p>\n<p>\u25a0\u7528\u6cd5<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<br \/>\n        [TestMethod]<br \/>\n        public void TestMethod1()<br \/>\n        {<br \/>\n            string[] lst = {<br \/>\n                &quot;masuda&quot;,<br \/>\n                &quot;tomoaki&quot;,<br \/>\n                &quot;yamasaki&quot;,<br \/>\n                &quot;yumi&quot; };<\/p>\n<p>            var q = from t in lst<br \/>\n                    where t.IsMatch(&quot;y.*i&quot;)<br \/>\n                    select t;<\/p>\n<p>            Assert.AreEqual(2, q.Count());<br \/>\n            Assert.AreEqual(&quot;yamasaki&quot;, q.First());<br \/>\n        }<\/p>\n<p>        [TestMethod]<br \/>\n        public void TestMethod2()<br \/>\n        {<br \/>\n            string[] lst = {<br \/>\n                &quot;masuda&quot;,<br \/>\n                &quot;tomoaki&quot;,<br \/>\n                &quot;yamasaki&quot;,<br \/>\n                &quot;yumi&quot; };<\/p>\n<p>            var q = from t in lst<br \/>\n                    where t.IsMatch(&quot;ki$&quot;)<br \/>\n                    select new { match = t.Match(&quot;..ki$&quot;) };<\/p>\n<p>            Assert.AreEqual(2, q.Count());<br \/>\n            Assert.AreEqual(&quot;oaki&quot;, q.First().match);<br \/>\n        }<br \/>\n[\/code]<\/p>\n<p>\u5358\u7d14\u306b\u3001Regex \u306e Match, IsMatch \u30e1\u30bd\u30c3\u30c9\u3092 String \u30af\u30e9\u30b9\u306b\u304f\u3063\u3064\u3051\u305f\u3060\u3051\u306a\u306e\u3067\u3059\u304c\u3001LINQ \u5185\u3067\u76f4\u63a5\u4f7f\u3048\u308b\u3068\u3044\u3046\u306e\u304c\u30e1\u30ea\u30c3\u30c8\u3067\u3059\u306d\u3002\u5225\u9014\u3001new Regex(&#8230;) \u3092\u4f7f\u3048\u3070\u3001LINQ \u5185\u3067\u3082\u66f8\u3051\u308b\u306e\u3059\u304c\u3001\u305d\u308c\u3060\u3068\u6587\u304c\u9577\u304f\u306a\u3063\u3066\u3057\u307e\u3046\u3057\u3068\u3044\u3046\u30d1\u30bf\u30fc\u30f3\u3067\u3059\u3002<\/p>\n<p>\u25a0\u5b9f\u88c5<\/p>\n<p>\u5b9f\u88c5\u306f\u304a\u305d\u308d\u3057\u304f\u7c21\u5358\u3067\u4ee5\u4e0b\u306a\u611f\u3058\u3002\u5358\u7d14\u306b Match, IsMatch \u3092\u30e9\u30c3\u30d7\u3057\u307e\u3059\u3002<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<br \/>\nusing System.Text.RegularExpressions;<\/p>\n<p>namespace Moonmile.StringRegex<br \/>\n{<br \/>\n    public static class StringRegexExtentions<br \/>\n    {<br \/>\n        public static string Match( this string target, string pattern )<br \/>\n        {<br \/>\n            var rx = new Regex(pattern);<br \/>\n            var mc = rx.Matches(target);<br \/>\n            return (mc.Count == 0) ? &quot;&quot; : mc[0].Value;<br \/>\n        }<br \/>\n        public static bool IsMatch(this string target, string pattern)<br \/>\n        {<br \/>\n            var rx = new Regex(pattern);<br \/>\n            return rx.IsMatch( target );<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>\u25a0\u5229\u7528\u65b9\u6cd5<\/p>\n<p>\u3067\u3001\u6700\u7d42\u7684\u306b\u306f\u4f55\u3092\u3057\u305f\u3044\u306e\u304b\u3068\u3044\u3046\u3001\u4ee5\u4e0b\u306a\u611f\u3058\u3067\uff12\u3064\u306e\u30d5\u30a1\u30a4\u30eb\u306e DIFF \u3092\u8abf\u3079\u305f\u304b\u3063\u305f\u306e\u3067\u3059\u3002<br \/>\n\uff12\u3064\u306e\u30d5\u30a1\u30a4\u30eb\u306b\u3001\u30d5\u30a1\u30a4\u30eb\u540d\u306e\u7f85\u5217\u304c\u3042\u3063\u3066\u3001\u305d\u308c\u3092\u4f7f\u3063\u3066\u540c\u671f\u3092\u3059\u308b\u30c4\u30fc\u30eb\u3067\u3059\u306d\u3002\u30de\u30c3\u30c1\u3055\u305b\u305f\u3044\u62e1\u5f35\u5b50\u304c\u9650\u3089\u308c\u3066\u3044\u308b\u306e\u3067\u305d\u308c\u3092\u5185\u90e8\u3067\u6307\u5b9a\u3059\u308b\u3068\u3044\u3046\u73fe\u5834\u306e\u5373\u3057\u305f\u30c4\u30fc\u30eb\u3067\u3059\u3002<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<br \/>\nstatic void Main(string[] args)<br \/>\n{<br \/>\n#if false<br \/>\n\tif (args.Length != 2)<br \/>\n\t{<br \/>\n\t    Console.WriteLine(&quot;usage: difffolders [src] [dest]&quot;);<br \/>\n\t    return;<br \/>\n\t}<br \/>\n\tvar lsrc = new List();<br \/>\n\tvar ldst = new List();<\/p>\n<p>\tStreamReader sr = File.OpenText(src);<br \/>\n\twhile (!sr.EndOfStream)<br \/>\n\t    lsrc.Add(sr.ReadLine());<br \/>\n\tsr.Close();<br \/>\n\tsr = File.OpenText(dest);<br \/>\n\twhile (!sr.EndOfStream)<br \/>\n\t    ldst.Add(sr.ReadLine());<br \/>\n\tsr.Close();<\/p>\n<p>\tvar llsrc = new List();<br \/>\n\tvar lldst = new List();<\/p>\n<p>\tstring[] exts = {<br \/>\n\t                    &quot;\\\\.cpp&quot;, &quot;\\\\.h&quot;, &quot;\\\\.hpp&quot;, &quot;\\\\.rc&quot;, &quot;\\\\.h&quot;, &quot;\\\\.f90&quot;, &quot;\\\\.fi&quot;, &quot;\\\\.for&quot;, &quot;\\\\.inc&quot;, &quot;\\\\.cmn&quot;<br \/>\n\t                    };<br \/>\n\tforeach (var ext in exts)<br \/>\n\t{<br \/>\n\t    string ext1 = ext + &quot;$&quot;;<br \/>\n\t    llsrc.AddRange(lsrc.Where(n =&gt; n.IsMatch(ext1) == true).Select(n =&gt; n.Replace(&quot;C:\\\\Develop\\\\Main-branch-0&quot;,&quot;&quot;)));<br \/>\n\t    lldst.AddRange(ldst.Where(n =&gt; n.IsMatch(ext1) == true).Select(n =&gt; n.Replace(&quot;C:\\\\Develop\\\\Main-branch-1&quot;,&quot;&quot;)));<br \/>\n\t}<br \/>\n\tllsrc.RemoveAll(n =&gt; n.IsMatch(&quot;\\\\Debug&quot;)); ;<br \/>\n\tlldst.RemoveAll(n =&gt; n.IsMatch(&quot;\\\\Debug&quot;));<br \/>\n\tllsrc.Sort();<br \/>\n\tlldst.Sort();<\/p>\n<p>\t\/\/ \u5dee\u5206\u3092\u8a08\u7b97\u3059\u308b<br \/>\n\tDebug.Print(&quot;src count: {0}&quot;, llsrc.Count);<br \/>\n\tDebug.Print(&quot;dst count: {0}&quot;, lldst.Count);<\/p>\n<p>\tvar onlysrc = llsrc.Where(n =&gt; !lldst.Contains(n)).Select(n =&gt; n );<br \/>\n\tvar onlydst = lldst.Where(n =&gt; !llsrc.Contains(n)).Select(n =&gt; n );<br \/>\n\tvar andlst = llsrc.Where(n =&gt; lldst.Contains(n)).Select(n =&gt; n);<\/p>\n<p>\tDebug.Print(&quot;only\/and {0} {1} {2}&quot;, onlysrc.Count(), onlydst.Count(), andlst.Count());<\/p>\n<p>\tStreamWriter sw = new StreamWriter(File.OpenWrite(&quot;out-diff.txt&quot;));<br \/>\n\tforeach( var f in onlysrc )<br \/>\n\t    sw.WriteLine(&quot;- {0}&quot;, f );<br \/>\n\tforeach (var f in onlydst)<br \/>\n\t    sw.WriteLine(&quot;+ {0}&quot;, f);<br \/>\n\tforeach (var f in andlst)<br \/>\n\t    sw.WriteLine(&quot;m {0}&quot;, f);<br \/>\n\tsw.Close();<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>\u3067\u3001\u3053\u308c\u3092\u4f5c\u308b\u3046\u3048\u3067\u56f0\u3063\u305f\u306e\u304c\u3001.NET \u306e\u6b63\u898f\u8868\u73fe\u306e\u307e\u305a\u3055\uff08Regex\u306e\u5b9f\u88c5\u304b\u306a\uff1f\uff09\u3067\u3059\u306d\u3002\u672c\u6765\u306a\u3089\u3070\u3001<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<br \/>\nvar q = llsrc.Where( n =&gt; n.IsMatch(&quot;\\\\.(cpp|h|hpp|rc|f98|for|fi|inc|cmn)&quot;).Select( n =&gt; n );<br \/>\n[\/code]<\/p>\n<p>\u306e\u3088\u3046\u306b\u4e00\u767a\u3067\u66f8\u304d\u305f\u3044\u3068\u3053\u308d\u306a\u306e\u3067\u3059\u304c\u3001.NET \u6b63\u898f\u8868\u73fe\u3067\u306f\u3001OR \u6f14\u7b97\u5b50\u304c\u7121\u3044\u6a21\u69d8\u3002Group \u3092\u4f7f\u3046\u3093\u3067\u3059\u304b\u306d\uff1f<\/p>\n<p>2012\/09\/18 \u8ffd\u8a18<\/p>\n<p>\u3068\u304b\u601d\u3063\u305f\u3089\u3001<\/p>\n<p>\u4ee3\u66ff\u69cb\u6210\u4f53<br \/>\n<a href=\"http:\/\/msdn.microsoft.com\/ja-jp\/library\/36xybswe(v=vs.100).aspx\">http:\/\/msdn.microsoft.com\/ja-jp\/library\/36xybswe(v=vs.100).aspx<\/a><\/p>\n<p>\u306b\u300c|\u300d\u304c\u4f7f\u3048\u308b\u3084\u3093\u30c3!!! \u3063\u3066\u3053\u3068\u3067\u3001\u591a\u5206\u5148\u306e\u30b3\u30fc\u30c9\u306f\u66f8\u3051\u308b\u30cf\u30ba\u3002\u5f8c\u3067\u66f8\u304d\u76f4\u3057\u307e\u3059\u304b\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u6b63\u898f\u8868\u73fe\u30af\u30e9\u30b9<br \/>\n<a href=\"http:\/\/msdn.microsoft.com\/ja-jp\/library\/30wbz966(v=vs.80)\">http:\/\/msdn.microsoft.com\/ja-jp\/library\/30wbz966(v=vs.80)<\/a><\/p>\n<p>\u4ed5\u65b9\u304c\u306a\u3044\u306e\u3067 array \u3067\u7528\u610f\u3057\u3066 foreach \u3067\u5efb\u3057\u3066\u3044\u307e\u3059\u3002Perl \u4e92\u63db\u306e\u6b63\u898f\u8868\u73fe\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u3042\u308c\u3070\u305d\u308c\u3092\u4f7f\u3044\u305f\u3044\u306a\u3068\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u3001\u30d5\u30a1\u30a4\u30eb\u540d\u304b\u3089\u62e1\u5f35\u5b50\u3092\u53d6\u308b Path.GetExtension \u3092\u4f7f\u3063\u3066\u3082\u826f\u3044\u3093\u3067\u3059\u3051\u3069\u306d\u3002<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<br \/>\n\tstring[] exts = {<br \/>\n            &quot;.cpp&quot;, &quot;.h&quot;, &quot;.hpp&quot;, &quot;.rc&quot;, &quot;.h&quot;, &quot;.f90&quot;, &quot;.fi&quot;, &quot;.for&quot;, &quot;.inc&quot;, &quot;.cmn&quot;<br \/>\n            };<\/p>\n<p>\tforeach ( var n in lsrc ) {<br \/>\n\t\tif (exts.Contains(Path.GetExtension(n)))<br \/>\n\t\t{<br \/>\n\t\t\tllsrc.Add(n.Replace(&quot;C:\\\\Develop\\\\Main-branch-0&quot;, &quot;&quot;));<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\tforeach ( var n in ldst ) {<br \/>\n\t\tif (exts.Contains(Path.GetExtension(n)))<br \/>\n\t\t{<br \/>\n\t\t\tlldst.Add(n.Replace(&quot;C:\\\\Develop\\\\Main-branch-1&quot;, &quot;&quot;));<br \/>\n\t\t}<br \/>\n\t}<br \/>\n[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>System::String \u306b\u6b63\u898f\u8868\u73fe\u304c\u4f7f\u3048\u305f\u3089\u3044\u3044\u306a\u3041\u3068\u601d\u3044\u3064\u3064\u3001\u66f8\u3044\u3066\u307f\u305f\u306e\u304c\u3053\u308c\u3067\u3059\u3002 \u25a0\u7528\u6cd5 [code lang=&#8221;csharp&#8221;] [TestMethod] public void  &hellip; <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/3717\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[23],"tags":[],"class_list":["post-3717","post","type-post","status-publish","format-standard","hentry","category-csharp"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/3717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=3717"}],"version-history":[{"count":4,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/3717\/revisions"}],"predecessor-version":[{"id":3719,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/3717\/revisions\/3719"}],"wp:attachment":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=3717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=3717"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=3717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}