-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 CloudyまたはNegative

C# または VB.NET でそれを行う方法は?

4

4 に答える 4

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 に答える
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 に答える
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 に答える
0

次の正規表現を使用できます

(?<=fs22\s*)[^}]+(?=}$)
于 2013-02-07T10:41:31.697 に答える