1

我似乎无法弄清楚如何从说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

非常感谢任何帮助!

4

2 回答 2

2

你可以试试

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

于 2012-12-28T05:47:08.433 回答
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 年特别匹配年份 [这可以从大多数电影标题中获取年份值,这减少了匹配的垃圾数据。考虑一下这是为了满足您现在的需求:)]。

于 2012-12-28T06:39:47.597 回答