C# 获取介于值和符号之间的文本.NET

C# 获取介于值和符号之间的文本.NET,c#,regex,vb.net,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是文本格式(大小、字体…)。 我对这个不感兴趣 我只想提取文本/字符串负数,但同一列可能还包含: {\rtf1\ansi\deff0{\fo

我在数据库(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
是文本格式(大小、字体…)。
我对这个不感兴趣

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

{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}}\f0\fs22 Slightly Cloudy}
换句话说,我只想获取
fs22
}

预期结果:
略微多云
阴性


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

您可以使用正则表达式:

(?<=\\fs22 )[^}]+(?=})
或在VB中:

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

Dim value=Regex.Match,”(?您可以使用以下Regex

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

(?使用子字符串有什么问题

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
我认为子字符串在性能方面也可能更好。我认为正则表达式不是实现简单字符串操作的最有效的方法。

[解决方案]
'带有正则表达式

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
Private Function\u compareText和String(ByVal regexp作为String,ByVal\u thetexttosearch作为String)作为String
Dim EXPreg作为System.Text.RegularExpressions.Regex
'1º-正则表达式
Dim expresoregular As String=regexp

“EX:”(?它的代码也更多,更难阅读和理解(好的,这来自于通常可以轻松阅读正则表达式的人)而且对更改的抵抗力较低,例如,如果开始标记更改为
\fs122
,您需要记住将
6
的所有实例更改为
7
。尽管如此,我还是会用更多的数据尝试这两种解决方案,并决定哪一种更快,但任何一种都解决了我的问题all@Flavius69很容易用两种方式定义分隔符分离字符串并测试这些字符串的长度,而不是神奇的数字6和7,但上面的示例更容易理解,我只是将其作为解决OP问题的替代方法,而不是使用正则表达式。
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
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