2016-05-27 71 views
-3

我正在寫一個小程序,以提取其季節的電視系列名稱。 我有以下文本字符串列表。正則表達式或LINQ匹配使用C#模式

 
Entourage Season 3 Part 2 | 5 NIGHT HIRE | 
Entourage Season 4 | 5 NIGHT HIRE | 
Entourage Season 5 | 5 NIGHT HIRE | 
Entourage Season 8 | 5 NIGHT HIRE | 
The Walking Dead: Season Four | 5 NIGHT HIRE | 
The Walking Dead: Season Three| 5 NIGHT HIRE | 
The Walking Dead: Season Two | 5 NIGHT HIRE | 
The Walking Dead: Season One | 5 NIGHT HIRE | 
Game Of Thrones: Season One| 5 NIGHT HIRE | 
Game Of Thrones: Season Two | 5 NIGHT HIRE | 

我需要按季節獲得電視劇名稱組。

的Entourage

 
Season 3 
Season 4 
Season 5 
Season 8 

行屍走肉

 
Season One 
Season Two 
Season Three 
Season Four 

權力的遊戲

 
Season One 
Season Two 

我有正則表達式匹配賽季,但它沒有奏效。

匹配匹配= Regex.Match(this.Content,@ 「(:^ |(?:?[。!?]?\ S))(季\ w +)」)

我需要幫助在C#中使用regualr表達式或使用LINQ查詢。

+2

這裏沒有問題;你正在給我們一個任務來爲你做。這不是如何工作。 – Rob

+0

哥哥你要求爲你做任務.. – DDave

+0

對不起兄弟,我只需要一行正則表達式(不是整個代碼),我不能寫。我認爲我需要學習正則表達式模式。謝謝你提醒我。 – MikMark

回答

2

這linqpad腳本:

var text = @" 
    Entourage Season 3 Part 2 | 5 NIGHT HIRE | 
    Entourage Season 4 | 5 NIGHT HIRE | 
    Entourage Season 5 | 5 NIGHT HIRE | 
    Entourage Season 8 | 5 NIGHT HIRE | 
    The Walking Dead: Season Four | 5 NIGHT HIRE | 
    The Walking Dead: Season Three| 5 NIGHT HIRE | 
    The Walking Dead: Season Two | 5 NIGHT HIRE | 
    The Walking Dead: Season One | 5 NIGHT HIRE | 
    Game Of Thrones: Season One| 5 NIGHT HIRE | 
    Game Of Thrones: Season Two | 5 NIGHT HIRE |"; 
var matches = text.Split(new[]{"\r\n","\n"},0) 
        .Select(l => Regex.Match(l, @"^\s*(?<title>.+?)(?<season>Season \w+)")); 
var data = matches.Where (m => m.Success) 
        .Select (m => new {Title = m.Groups["title"].Value.Trim(':',' '),Season = m.Groups["season"].Value}); 

data.GroupBy(d => d.Title) 
    .Select (g => new {g.First().Title,Seasons = g.Select (x => x.Season)}).Dump(); 

返回此:

enter image description here

+0

謝謝,正則表達式^ \ s *(?。+?)(?<season> Season \ w +)工作就像一個魅力。感謝您的輸出。 – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/415829/">MikMark</a></span> <span></span> </small> </span> </p> </div> </div> </div> </div> </div> </article> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1038284119" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="clearfix"> </div> <div class="relative-box"> <div class="relative">相關問題</div> <ul class="relative_list"> <li> 1. <a href="http://hk.uwenku.com/question/p-kqabxmey-pv.html" target="_blank" title="C#正則表達式模式匹配"> C#正則表達式模式匹配 </a> </li> <li> 2. <a href="http://hk.uwenku.com/question/p-qqtgeixm-uh.html" target="_blank" title="正則表達式c#模式匹配"> 正則表達式c#模式匹配 </a> </li> <li> 3. <a href="http://hk.uwenku.com/question/p-eoxhtecw-mp.html" target="_blank" title="匹配或使用正則表達式"> 匹配或使用正則表達式 </a> </li> <li> 4. <a href="http://hk.uwenku.com/question/p-dznesdfk-zd.html" target="_blank" title="匹配正則表達式...使用C#"> 匹配正則表達式...使用C# </a> </li> <li> 5. <a href="http://hk.uwenku.com/question/p-tdgeujag-eq.html" target="_blank" title="正則表達式(正則表達式)模式匹配"> 正則表達式(正則表達式)模式匹配 </a> </li> <li> 6. <a href="http://hk.uwenku.com/question/p-frahpizj-bev.html" target="_blank" title="C#使用字典替換正則表達式匹配模式"> C#使用字典替換正則表達式匹配模式 </a> </li> <li> 7. <a href="http://hk.uwenku.com/question/p-nafjmbvd-cw.html" target="_blank" title="Java模式匹配正則表達式"> Java模式匹配正則表達式 </a> </li> <li> 8. <a href="http://hk.uwenku.com/question/p-wxrkfaqj-gw.html" target="_blank" title="正則表達式不匹配模式"> 正則表達式不匹配模式 </a> </li> <li> 9. <a href="http://hk.uwenku.com/question/p-bxfgvynp-sa.html" target="_blank" title="REGEX - 正則表達式模式匹配"> REGEX - 正則表達式模式匹配 </a> </li> <li> 10. <a href="http://hk.uwenku.com/question/p-wayhgqkj-uv.html" target="_blank" title="URI正則表達式模式匹配"> URI正則表達式模式匹配 </a> </li> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-6208739752673518" data-ad-slot="4606349252"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <li> 11. <a href="http://hk.uwenku.com/question/p-utbkauax-ben.html" target="_blank" title="正則表達式匹配模式"> 正則表達式匹配模式 </a> </li> <li> 12. <a href="http://hk.uwenku.com/question/p-tbcnfetc-bmx.html" target="_blank" title="匹配模式正則表達式coldfusion"> 匹配模式正則表達式coldfusion </a> </li> <li> 13. <a href="http://hk.uwenku.com/question/p-gvtyylwk-e.html" target="_blank" title="模式匹配的正則表達式"> 模式匹配的正則表達式 </a> </li> <li> 14. <a href="http://hk.uwenku.com/question/p-cexigalj-uv.html" target="_blank" title="Python正則表達式 - 模式匹配"> Python正則表達式 - 模式匹配 </a> </li> <li> 15. <a href="http://hk.uwenku.com/question/p-qqkffjkd-vy.html" target="_blank" title="Java正則表達式匹配模式"> Java正則表達式匹配模式 </a> </li> <li> 16. <a href="http://hk.uwenku.com/question/p-pihgdiib-xk.html" target="_blank" title="正則表達式匹配模式"> 正則表達式匹配模式 </a> </li> <li> 17. <a href="http://hk.uwenku.com/question/p-zqgfcfwx-xq.html" target="_blank" title="Php正則表達式,匹配模式?"> Php正則表達式,匹配模式? </a> </li> <li> 18. <a href="http://hk.uwenku.com/question/p-dplpymqe-vr.html" target="_blank" title="Groovy正則表達式/模式匹配"> Groovy正則表達式/模式匹配 </a> </li> <li> 19. <a href="http://hk.uwenku.com/question/p-mpizczas-cp.html" target="_blank" title="匹配正則表達式模式"> 匹配正則表達式模式 </a> </li> <li> 20. <a href="http://hk.uwenku.com/question/p-amytheyd-hx.html" target="_blank" title="正則表達式模式不匹配"> 正則表達式模式不匹配 </a> </li> <li> 21. <a href="http://hk.uwenku.com/question/p-vsecdmzk-pm.html" target="_blank" title="php正則表達式匹配模式"> php正則表達式匹配模式 </a> </li> <li> 22. <a href="http://hk.uwenku.com/question/p-rkzcuewe-bdh.html" target="_blank" title="正則表達式匹配此模式"> 正則表達式匹配此模式 </a> </li> <li> 23. <a href="http://hk.uwenku.com/question/p-rznzbqsi-bdw.html" target="_blank" title="正則表達式模式匹配"> 正則表達式模式匹配 </a> </li> <li> 24. <a href="http://hk.uwenku.com/question/p-zkzwqnrs-cs.html" target="_blank" title="Java正則表達式模式匹配"> Java正則表達式模式匹配 </a> </li> <li> 25. <a href="http://hk.uwenku.com/question/p-hhjipqur-po.html" target="_blank" title="正則表達式匹配[/,。] ng模式"> 正則表達式匹配[/,。] ng模式 </a> </li> <li> 26. <a href="http://hk.uwenku.com/question/p-knixvpjq-bcw.html" target="_blank" title="模式匹配 - 正則表達式"> 模式匹配 - 正則表達式 </a> </li> <li> 27. <a href="http://hk.uwenku.com/question/p-ddoapbcl-vq.html" target="_blank" title="正則表達式 - 不匹配模式"> 正則表達式 - 不匹配模式 </a> </li> <li> 28. <a href="http://hk.uwenku.com/question/p-sgzwavmj-bek.html" target="_blank" title="匹配模式:正則表達式 - perl"> 匹配模式:正則表達式 - perl </a> </li> <li> 29. <a href="http://hk.uwenku.com/question/p-wbkpmopq-uu.html" target="_blank" title="Java正則表達式匹配模式"> Java正則表達式匹配模式 </a> </li> <li> 30. <a href="http://hk.uwenku.com/question/p-pocscwkw-xs.html" target="_blank" title="正則表達式匹配模式"> 正則表達式匹配模式 </a> </li> </ul> </div> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1575177025"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="padding-top-10"></div> </div> </div> <script type="text/javascript" src="http://img.uwenku.com/uwenku/script/side.js?t=1644592048261"></script> <script type="text/javascript" src="http://img.uwenku.com/uwenku/plugin/highlight/highlight.pack.js"></script> <link href="http://img.uwenku.com/uwenku/plugin/highlight/styles/docco.css" media="screen" rel="stylesheet" type="text/css" /> <script type="text/javascript"> $('pre').each(function(i, e) { hljs.highlightBlock(e, "<span class='indent'> </span>", false) }); </script> <div class="col-lg-3 col-md-4 col-sm-5"> <div id="rightTop"> <div class="row"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="5415218910" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="row sidebar panel panel-default"> <div class="panel-heading font-bold"> 最新問題 </div> <div class="m-b-sm m-t-sm clearfix"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-yjetlghp-mv.html" target="_blank" title="螺栓中的類型錯誤CMS"> 螺栓中的類型錯誤CMS </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-kqqxplja-ma.html" target="_blank" title="如何讀取R中的多個csv文件,並使用fread函數跳過最後一行"> 如何讀取R中的多個csv文件,並使用fread函數跳過最後一行 </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-bqpdgxhm-hn.html" target="_blank" title="從Flink 1.3.2升級到1.4.0 hadoop文件系統和路徑問題"> 從Flink 1.3.2升級到1.4.0 hadoop文件系統和路徑問題 </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-rsquypsa-ke.html" target="_blank" title="D3.js - 嵌套數據的作品,但選擇不遵循"> D3.js - 嵌套數據的作品,但選擇不遵循 </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-oljbtgar-dt.html" target="_blank" title="卡桑德拉CodecNotFoundException:編解碼器找不到請求的操作"> 卡桑德拉CodecNotFoundException:編解碼器找不到請求的操作 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-hzfpqyed-eo.html" target="_blank" title="爲什麼我不能打印()?"> 爲什麼我不能打印()? </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-kgtdfutl-gg.html" target="_blank" title="圖中不顯示閃亮"> 圖中不顯示閃亮 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-eahichhw-q.html" target="_blank" title="Django的css文件不起作用"> Django的css文件不起作用 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-wtxobtnw-bk.html" target="_blank" title="無法從孩子"> 無法從孩子 </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-cecgwetc-bu.html" target="_blank" title="在頁面加載時觸發Javascript文本和圖片預覽"> 在頁面加載時觸發Javascript文本和圖片預覽 </a> </li> </ul> </div> </div> </div> <p class="article-nav-bar"></p> <div class="row sidebar article-nav"> <div class="row box_white visible-sm visible-md visible-lg margin-zero"> <div class="top"> <h3 class="title"><i class="glyphicon glyphicon-th-list"></i> 相關問題</h3> </div> <div class="article-relative-content"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-kqabxmey-pv.html" target="_blank" title="C#正則表達式模式匹配"> C#正則表達式模式匹配 </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-qqtgeixm-uh.html" target="_blank" title="正則表達式c#模式匹配"> 正則表達式c#模式匹配 </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-eoxhtecw-mp.html" target="_blank" title="匹配或使用正則表達式"> 匹配或使用正則表達式 </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-dznesdfk-zd.html" target="_blank" title="匹配正則表達式...使用C#"> 匹配正則表達式...使用C# </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-tdgeujag-eq.html" target="_blank" title="正則表達式(正則表達式)模式匹配"> 正則表達式(正則表達式)模式匹配 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-frahpizj-bev.html" target="_blank" title="C#使用字典替換正則表達式匹配模式"> C#使用字典替換正則表達式匹配模式 </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-nafjmbvd-cw.html" target="_blank" title="Java模式匹配正則表達式"> Java模式匹配正則表達式 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-wxrkfaqj-gw.html" target="_blank" title="正則表達式不匹配模式"> 正則表達式不匹配模式 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-bxfgvynp-sa.html" target="_blank" title="REGEX - 正則表達式模式匹配"> REGEX - 正則表達式模式匹配 </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-wayhgqkj-uv.html" target="_blank" title="URI正則表達式模式匹配"> URI正則表達式模式匹配 </a> </li> </ul> </div> </div> </div> </div> </div> </div> </div><!-- wrap end--> <!-- footer --> <footer id="footer"> <div class="bg-simple lt"> <div class="container"> <div class="row padder-v m-t"> <div class="col-xs-8"> <ul class="list-inline"> <li><a href="http://hk.uwenku.com/contact">聯系我們</a></li> <li>© 2020 HK.UWENKU.COM</li> <li><a target="_blank" href="https://beian.miit.gov.cn/">沪ICP备13005482号-4</a></li> <li><script type="text/javascript" src="https://v1.cnzz.com/z_stat.php?id=1280101193&web_id=1280101193"></script></li> <li><a href="http://www.uwenku.com/" target="_blank" title="优文库">简体中文</a></li> <li><a href="http://hk.uwenku.com/" target="_blank" title="優文庫">繁體中文</a></li> <li><a href="http://ru.uwenku.com/" target="_blank" title="поле вопросов и ответов">Русский</a></li> <li><a href="http://de.uwenku.com/" target="_blank" title="Frage - und - antwort - Park">Deutsch</a></li> <li><a href="http://es.uwenku.com/" target="_blank" title="Preguntas y respuestas">Español</a></li> <li><a href="http://hi.uwenku.com/" target="_blank" title="कार्यक्रम प्रश्न और उत्तर पार्क">हिन्दी</a></li> <li><a href="http://it.uwenku.com/" target="_blank" title="IL Programma di chiedere Park">Italiano</a></li> <li><a href="http://ja.uwenku.com/" target="_blank" title="プログラム問答園区">日本語</a></li> <li><a href="http://ko.uwenku.com/" target="_blank" title="프로그램 문답 단지">한국어</a></li> <li><a href="http://pl.uwenku.com/" target="_blank" title="program o park">Polski</a></li> <li><a href="http://tr.uwenku.com/" target="_blank" title="Program soru ve cevap parkı">Türkçe</a></li> <li><a href="http://vi.uwenku.com/" target="_blank" title="Đáp ứng viên">Tiếng Việt</a></li> <li><a href="http://fr.uwenku.com/" target="_blank" title="Programme interrogation Park">Française</a></li> </ul> </div> </div> </div> </div> </div> </footer> <!-- / footer --> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?f78a970f17b19a79fc477a3378096f29"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>