-1

我在数据库(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文本格式(大小,字体...)在哪里。
我对此不感兴趣。

我只想提取 text / string Negative,但同一列可能还包含:

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

换句话说,我只想抓取和之间的fs22文本}

预期结果:Slightly CloudyNegative

如何在 C# 或 VB.NET 中执行此操作?

4

4 回答 4

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
于 2013-02-07T10:39:53.120 回答
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 在性能方面也可能更好。我认为正则表达式不是处理简单字符串操作的最有效方法。

于 2013-02-07T10:55:48.103 回答
0

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

(?<=fs22\s*)[^}]+(?=}$)
于 2013-02-07T10:41:31.697 回答
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
于 2013-02-07T18:55:16.303 回答