下面是我用来从用户 ID 中提取名字的表达式
即"text\[forename] [Surname]"
目前,如果“”位于名字和姓氏之间,则此表达式是合适的。然而,我注意到的是“”可以被“.”代替。或例如“/”或“_”。
我遇到的麻烦是包含多个“Mid”函数以包含所有这些选项。我曾尝试使用“Or”、“Switch”和许多其他运算符,但都没有效果。
我没有使用数据集,而是报告中的参数,即当用户登录报告时,他们的 UserID 被用作参数。
我是否漏掉了一些明显的东西,或者我是否完全漏掉了标记?
如有任何帮助,我们将不胜感激。
= "Hi " + Mid(Parameters!User.Value, InStr(Parameters!User.Value,"\")+1, (InStr(Parameters!User.Value, " ") - InStr(Parameters!User.Value, "\") - 1)) + "," &
chr(10) & chr(13) &
chr(10) & chr(13) &
"You have access to the Teams listed below. Only data from these teams will be shown in any analysis. Please click above to continue to the Dashboards"
最佳答案
正则表达式绝对是解决此类问题的方法。这是一个如何使用正则表达式提取名字的示例:
Dim input As String = Parameters!User.Value
Dim pattern As String = "(?<=\\)[^ ./_]+"
Dim forename As String = Regex.Match(input, pattern).Value
Dim output As String = $"Hi {forename},
You have access to the Teams listed below. Only data from these teams will be shown in any analysis. Please click above to continue to the Dashboards"
VB.NET 现在允许多行字符串文字,因此无需使用 Chr(10) & Chr(13)
。即使您不使用多行文字,根据您的需要使用 Environment.NewLine
或 vbCrLf
也会更好。字符串文字前的 $
使其成为内插字符串,这意味着您可以在字符串中间的 {}
括号中插入变量值。如果你不想使用它,你可以像以前一样连接:
Dim output As String = "Hi " & forename &
Environment.NewLine &
Environment.NewLine &
"You have access to the Teams listed below. Only data from these teams will be shown in any analysis. Please click above to continue to the Dashboards"
正则表达式是搜索、解析和转换字符串的强大工具。如果您要进行大量数据相关工作,那么学习它是非常值得的。
如果您不想使用正则表达式,我建议您使用 String.Split
,因为该方法允许您传递任意数量的分隔符。例如,您可以这样做:
Dim input As String = Parameters!User.Value
Dim parts() As String = input.Split("\"c, " "c, "."c, "/"c, "_"c)
Dim forename As String = If(parts.Length >= 2, parts(1), Nothing)
关于vb.net - 在 SSRS 中对一个参数使用多个 VB Mid 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47476097/