2013-01-31 31 views
1

由於必須從結構根本無法預測的小型字符串中提取程序標題,導致出現問題。有一些模式可以在下面看到,並且每個字符串都必須經過評估,看它是否與任何這些結構相匹配,以便我能夠正確地獲得標題。使用RegEx從字符串中提取標題

我已經買了掌握正則表達式,但是我必須做到這一點的時間不允許我學習這本書,並試圖獲得必要的介紹(有趣但很特別)的主題。

Perharps,這方面經驗豐富的人可以幫助我理解如何完成這項工作?

Some random Name 2 - Ep.1 
=> Some random Name 2 

Some random Name - Ep.1 
=> Some random Name 

Boff another 2 name! - Ep. 228 
=> Boff another 2 name!  

Another one & the rest - T1 Ep. 2 
=>Another one & the rest 

T5 - Ep. 2 Another Name  
=> Another Name 

T3 - Ep. 3 - One More with an Hyfen 
=> One More with an Hyfen 

Another one this time with a Date - 02/12/2012 
=>Another one this time with a Date 

10 Aug 2012 - Some Other 2 - Ep. 2 
=> Some Other 2 

Ep. 93 - Some program name 
=> Some Program name  
Someother random name - Epis. 1 e 2 
=> Someother random name 

The Last one with something inside parenthesis (V.O.) 
=> The Last one with something inside parenthesis 

正如你可以看到從A-ZA-Z,我想從給定的字符串中提取可能有數字,特殊字符,如&的標題和文字(我想這是所有)

的複雜的部分來時,必須知道它是否有一個空格或更多的標題後面跟一個連字符,如果它有零或更多的空間,直到Ep。 (我無法解釋這一點,它只是複雜)

+0

這可能是不可能的;畢竟,你試圖提取的電影標題(我假設那些是電視劇)可能和它們文件名的格式一樣瘋狂。有時,即使是一個人也可能難以從這些電影中提取標題。假設你有'300 - 01'這樣的東西:顯然標題是300,而且是01集,或者它真的很明顯嗎? – Nolonar

+0

更何況'星際爭霸1「或」Starwars Ep。 1',在這種情況下,「Episode 1」或「Ep。 1'是標題的一部分... – Nolonar

+0

@Nolonar你是完全正確的!我期望的是在上面的每個模式上測試每個字符串,如果沒有匹配,就忽略程序。我已經在考慮這種情況。 – Lothre1

回答

1

該程序將處理您的案例。主要原則是,如果在字符串的開頭或結尾存在某個序列,則刪除該序列。如果要刪除的字符串的格式會發生更改,或者需要更改它們的順序,則必須維護正則表達式的列表。

using System; 
    using System.Text.RegularExpressions; 

    public class MyClass 
    { 


     static string [] strs = 
     {  
       "Some random Name 2 - Ep.1", 
       "Some random Name - Ep.1", 
       "Boff another 2 name! - Ep. 228", 
       "Another one & the rest - T1 Ep. 2", 
       "T5 - Ep. 2 Another Name", 
       "T3 - Ep. 3 - One More with an Hyfen", 
       @"Another one this time with a Date - 02/12/2012", 
       "10 Aug 2012 - Some Other 2 - Ep. 2", 
       "Ep. 93 - Some program name", 
       "Someother random name - Epis. 1 e 2", 
       "The Last one with something inside parenthesis (V.O.)"}; 

     static string [] regexes = 
     { 
      @"T\d+", 
      @"\-", 
      @"Ep(i(s(o(d(e)?)?)?)?)?\s*\.?\s*\d+(\s*e\s*\d+)*", 
      @"\d{2}\/\d{2}\/\d{2,4}", 
      @"\d{2}\s*[A-Z]{3}\s*\d{4}", 
      @"T\d+", 
      @"\-", 
      @"\!", 
      @"\(.+\)", 
     }; 

     public static void Main() 
     { 
      foreach(var str in strs) 
      { 
       string cleaned = str.Trim(); 
       foreach(var cleaner in regexes) 
       { 
        cleaned = Regex.Replace(cleaned, "^" + cleaner, string.Empty, RegexOptions.IgnoreCase).Trim(); 
        cleaned = Regex.Replace(cleaned, cleaner + "$", string.Empty, RegexOptions.IgnoreCase).Trim(); 
       } 
       Console.WriteLine(cleaned); 
      } 
      Console.ReadKey(); 
     } 
+0

真棒!聰明的解決方案。聰明的做法。 – Lothre1

0

如果這只是有關檢查的模式,而不是實際提取的標題名稱,讓我一展身手:

隨着@"Ep(is)?\.?\s*\d+"您可以檢查字符串例如「Ep1」,「Ep01」,「Ep.999」,「Ep3」,「Epis.0」,「Ep 11」等(它也檢測Ep和數字之間的多個空格)。 如果您想要匹配「ep1」以及「Ep1」或「EP1」,您可能需要使用RegexOptions.IgnoreCase

如果您確定,沒有名稱將包含「 - 」,並且該字符會分開從情節,信息名稱,你可以嘗試拆分字符串是這樣的:

string[] splitString = inputString.Split(new char[] {'-'}); 
foreach (string s in splitString) 
{ 
    s.Trim() // removes all leading or trailing whitespaces 
} 

你會在任何splitString[0]splitString[1]和其他情節,信息有姓名。

要搜索日期,您可以使用:@"\d{1,4}(\\|/|.|,)\d{1,2}(\\|/|.|,)\d{1,4}"可以檢測日期,前面或後面用1到4位小數表示的日期(除了中心值,可以是1到2位小數長)並用反斜線,斜線,逗號或點分隔。

就像我之前提到的:這會不會讓你的程序提取實際標題,只有找出是否存在這樣的字符串(這些字符串可能還是冠軍本身的一部分)

編輯:

一種擺脫多空格的方法是使用inputString = Regex.Replace(inputString, "\s+", " "),它用一個空格替換多個空格。也許你有下劃線而不是空格?如:「This_is_a_name」,在這種情況下,在刪除多個空格之前,您可能需要使用inputString = Regex.Replace(inputString, "_+", " ")

相關問題