删除之前的所有字符串:

时间:2014-01-05 08:54:21

标签: regex vb.net

"\:(.*)$"

大家好我正在使用上面的表达式删除:(冒号)之前的所有字符串,但它在此之前给了我所有的字符串。我怎样才能做到这一点。非常感谢。

我的字符串是:

This is text: Hi here we go

我得到:This is text

我想:Hi here we go

更新了代码

Sub Main()
    Dim input As String = "This is   text with   : far  too   much  "
    Dim pattern As String = "\:(.*)$"
    Dim replacement As String = " "
    Dim rgx As New Regex(pattern)
    Dim result As String = rgx.Replace(input, replacement)

    Console.WriteLine("Original String: {0}", input)
    ' MsgBox("Original String: {0}")
    Console.WriteLine("Replacement String: {0}", result)
    MsgBox("Original String: {0}")
End Sub

3 个答案:

答案 0 :(得分:0)

尝试这种模式。这将帮助您匹配冒号后的字符串

/ :()/

/ :(。+)/

答案 1 :(得分:0)

应该是:

Dim pattern As String = "(.*)\:"
' in vb if above one doesn't work, then try this one
' Dim pattern As String = "^(.*)\:"
' also i don't think we need to use any brackets here as well.

这个正则表达式意味着冒号(:)之前的任何内容,你在示例中使用冒号(:)之后的任何内容。

答案 2 :(得分:0)

如果你没有在RegEx上设置死机,那么你也可以使用

 Dim result As String
 result = Strings.Split(Input, ":", 2)(1)

这会将输入拆分为包含两个元素的数组。第一个元素是第一个“:”之前的文本,第二个元素是之后的文本。