在 RegEx 替换上替换 Stripbrackets
Replace Stripbrackets on RegEx Replace
我有一个带括号的输入字符串:“[3][4] at [5]”
这是我的 sql 查询的示例数据表结果:
我需要做的是替换第i列单元格对应的[i]。
输出应该是:
[3][4] 在 [5] = “15A 断路器 #348901836 at 19-Afalcon St. Capitol Subdivision”
这是我的代码:
Dim colstring As String
Dim arg As String
Dim remarks As String = "[3][4] at [5]" *this is from a textbox
While Not i > dtresults.Columns.Count - 1
rep = "[" & i & "]"
colstring = dtresults.Columns(i).ColumnName
arg = Regex.Replace(remarks, rep, colstring, RegexOptions.ExplicitCapture)
remarks = arg
i = i + 1
End While
问题是输出包含带状括号:“[15A 断路器] [#348901836] at [19-Afalcon St. Capitol Subdivision]”
如何使用 Regex.Replace 方法显式替换包含 stripbracket 的字符串?
首先,如果你想要table的特定行的列中的值,那么你不能像
那样将列名写为替换字符串
colstring = dtresults.Columns(i).ColumnName
改为使用
colstring = dtresults.Rows(0).Item(i).ToString
在开头的替换字符串模式中添加“\”,如下所示:
rep = "\[" & i & "]"
因此您的最终代码将如下所示:
Dim colstring As String
Dim arg As String
Dim remarks As String = "[3][4] at [5]" *this is from a textbox
While Not i > dtresults.Columns.Count - 1
rep = "\[" & i & "]"
colstring = dtresults.Rows(0).Item(i).ToString
arg = Regex.Replace(remarks, rep, colstring, RegexOptions.ExplicitCapture)
remarks = arg
i = i + 1
End While
我有一个带括号的输入字符串:“[3][4] at [5]”
这是我的 sql 查询的示例数据表结果:
我需要做的是替换第i列单元格对应的[i]。 输出应该是:
[3][4] 在 [5] = “15A 断路器 #348901836 at 19-Afalcon St. Capitol Subdivision”
这是我的代码:
Dim colstring As String
Dim arg As String
Dim remarks As String = "[3][4] at [5]" *this is from a textbox
While Not i > dtresults.Columns.Count - 1
rep = "[" & i & "]"
colstring = dtresults.Columns(i).ColumnName
arg = Regex.Replace(remarks, rep, colstring, RegexOptions.ExplicitCapture)
remarks = arg
i = i + 1
End While
问题是输出包含带状括号:“[15A 断路器] [#348901836] at [19-Afalcon St. Capitol Subdivision]”
如何使用 Regex.Replace 方法显式替换包含 stripbracket 的字符串?
首先,如果你想要table的特定行的列中的值,那么你不能像
那样将列名写为替换字符串colstring = dtresults.Columns(i).ColumnName
改为使用
colstring = dtresults.Rows(0).Item(i).ToString
在开头的替换字符串模式中添加“\”,如下所示:
rep = "\[" & i & "]"
因此您的最终代码将如下所示:
Dim colstring As String
Dim arg As String
Dim remarks As String = "[3][4] at [5]" *this is from a textbox
While Not i > dtresults.Columns.Count - 1
rep = "\[" & i & "]"
colstring = dtresults.Rows(0).Item(i).ToString
arg = Regex.Replace(remarks, rep, colstring, RegexOptions.ExplicitCapture)
remarks = arg
i = i + 1
End While