在值和符号.NET之间获取文本

时间:2013-02-07 10:38:08

标签: c# regex vb.net

我在数据库(Sql Server)中有一个包含如下值的列:

{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}}\f0\fs22 Negative}

其中\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}}\f0\fs22是文本格式(大小,字体...) 我对此并不感兴趣。

我只想提取文本/字符串Negative,但同一列也可能包含:

{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}}\f0\fs22 Slightly Cloudy}

换句话说,我想只抓取fs22}

之间的文字

预期结果:Slightly CloudyNegative

如何在C#或VB.NET中完成?

4 个答案:

答案 0 :(得分:1)

您可以使用正则表达式:

(?<=\\fs22 )[^}]+(?=})

这将匹配\fs22}之间的任何内容,而不在匹配中包含所述分隔符(这是使用外观断言实现的)。在C#中,这看起来像

var value = Regex.Match(s, @"(?<=\\fs22 )[^}]+(?=})").Value;

或在VB中:

Dim value = Regex.Match(s, "(?<=\\fs22 )[^}]+(?=})").Value

快速PowerShell测试:

PS> '{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}}\f0\fs22 Negative}',
>> '{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}}\f0\fs22 Slightly Cloudy}' |
>> %{ [Regex]::Match($_, '(?<=\\fs22 )[^}]+(?=})') }
>>


Groups   : {Negative}
Success  : True
Captures : {Negative}
Index    : 69
Length   : 8
Value    : Negative

Groups   : {Slightly Cloudy}
Success  : True
Captures : {Slightly Cloudy}
Index    : 69
Length   : 15
Value    : Slightly Cloudy

答案 1 :(得分:1)

使用SubString有什么问题?

string s = @"{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}}\f0\fs22 Negative}";
int i = s.LastIndexOf(@"\fs22 ");
string x = s.Substring(i + 6, s.Length - i - 6 - 1);
// 6 = length of string "\fs22 "
// 1 = minus the } at the end

我认为SubString也可能是更好的性能。我认为正则表达式不是处理简单字符串操作的最有效方法。

答案 2 :(得分:0)

您可以使用以下正则表达式

(?<=fs22\s*)[^}]+(?=}$)

答案 3 :(得分:0)

[解决方案已解决]
'用正则表达式

Private Function _CompareTextWithString(ByVal regexp As String, ByVal _theTextWhereToSearch As String) As String

    Dim EXPreg As System.Text.RegularExpressions.Regex

    '1º - The Regular Expression
    Dim expresaoREGULAR As String = regexp
    ' EX: "(?<=fs22\s*)[^}]+(?=}$)"

    '2º - Associate the expression to a Variavel Regex
    EXPreg = New System.Text.RegularExpressions.Regex(expresaoREGULAR, RegexOptions.IgnoreCase)
    '3º
    ' Check if matches with
    Dim check As Match = EXPreg.Match(_theTextWhereToSearch)

    If (check.Success) Then

        Return check.Value ' Matches
    Else
        Return False ' No Matches
    End If

End Function
'Usage
Private Sub _btExecRegex_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _btExecRegex.Click
    _txtResult.Text = _CompareTextWithString("(?<=fs22\s*)[^}]+(?=}$)", _
                                                "{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}}\f0\fs22 Negative}")
End Sub

'With Substring
Private Function _returnValueFromStr(ByVal _str As String, ByVal _strFilterLastIndexOf As String, ByVal _lastCharOrChars As Integer) As String
    'Last ocourence of the filter
    Dim i As Integer = _str.LastIndexOf(_strFilterLastIndexOf)
    'size of Filter
    Dim f As Integer = _strFilterLastIndexOf.Length

    'Return the value from _str wich is filtered 
    'with _strFilterLastIndexOf and at the end -1 (or -2 ...) the char i don't need

    Return _str.Substring(i + f, _str.Length - i - f - _lastCharOrChars)

End Function
'Usage
Private Sub _btExecutar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _btExecutarSubStr.Click
    _txtResult.Text = _returnValueFromStr("{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}}\f0\fs22 Negative}", _
                                             "\fs22 ", 1)
End Sub