[英]Get Year from String (e.g. Rogue Assassin 2007)
我似乎無法弄清楚如何從Rogue Assassin 2007
獲取年份並返回:
moviename = "Rogue Assassin" 'Whitespace doesn't matter
movieyear = "2007" 'Whitespace doesn't matter
但是,我無法讓它發揮作用。 我一直在嘗試以下方面:
If System.Text.RegularExpressions.Regex.IsMatch(fn, "[0-9][0-9][0-9][0-9]") Then 'Is this right?
Dim mtemp As String() = fn.Split(" ") 'Movie temp array
myear(0) = mtemp.Last 'Attempting to set year to the last split
For Each z As String In mtemp 'Adding the other elements in mtemp together to remake the title
If z IsNot mtemp.Last Then
mtitle(0) = z & " " 'Movie title
Else
Exit For
End If
Next
...
Else
...
End If
任何幫助非常感謝!
你可以試試
Dim r As Regex = new Regex("(.*)\s+([0-9]+)$");
Dim match As Match = System.Text.RegularExpressions.Regex.Match("Rogue Assassin 2007")
上面的代碼會將匹配捕獲為2組,然后您可以使用match.Groups(1).Captures(0).Value和match.Groups(1).Captures(1).Value檢索捕獲的匹配。
http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
1) Regular expression for matching year strings containing year 1800 to 2013 (ideal regex for obtaining movie year from the title)
1[89][0-9]{2}|200[0-9]|201[0-3]
2) Regular expression for matching year strings containing year from 1800 onwards.
1[89][0-9]{2}|20[0-9]{2}
Have tested the pattern (1) for the below movie titles:
Die Hard 2 1990 -> 1990
Django Unchained (2012) -> 2012
This Is 40 (2012) -> 2012
The Twilight Saga: Breaking Dawn - Part 2 - 2012 -> 2012
Die Hard 4.0 2007 -> 2007
假設:
由於您的問題中未指定年份格式,因此假定年份始終為4位數。
電影標題也可以包含其他4位數字,因此年份特別匹配從1800年到2013年[可以從大多數電影標題獲得年份值,這減少了匹配的垃圾數據。 考慮這是為了滿足您現在的需求:)]。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.