如何找到由周围下划线限定的 5 个数字的变量集?
How do I find a variable set of 5 numbers qualified by surrounding underscores?
我正在将文件名拉入一个变量 (@[User::FileName]
) 并尝试从该字符串中提取工单编号(始终是 5 个数字,两边都有下划线)。例如,文件名看起来像 - "ABC_2017_DEF_9_12_GHI_35132_S5160.csv"
。我希望结果为 return“35132”。我找到了如何做到这一点的例子,例如 SUBSTRING(FileName,1,FINDSTRING(FileName,"_",1) - 1)
但下划线不会总是在同一个位置。
是否可以在表达式构建器中执行此操作?
答案:
public void Main()
{
string strFilename = Dts.Variables["User::FileName"].Value.ToString();
var RegexObj = new Regex(@"_([\d]{5})_");
var match = RegexObj.Match(strFilename);
if (match.Success)
{
Dts.Variables["User::WorkOrder"].Value = match.Groups[1].Value;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
我会使用脚本转换(如果不在 DataFlow 中则使用脚本任务)并使用正则表达式。
其他部分有什么意义吗?
无论如何你可以使用脚本任务和拆分功能。
以只读方式传入@fileName,以读写方式传入@WO
string fn = Dts.Variables["fileName"].Value;
string[] parts = fn.Split('_');
//Assuming it's always the 7th part
// You could extract the other parts as well.
Dts.Variables["WO"].Value = part(6);
首先,您提供的示例 ABC_2017_DEF_9_12_GHI_35132_S5160.csv
包含位于下划线之间的 4 个数字:
2017 , 9 , 12 , 35132
我不知道文件名是否包含很多个 5 位数字可以出现很多次,所以在我的回答中我会假设你想要 return 的数字是最后一次出现的数字由 5 个数字组成。
解决方案
您必须使用以下正则表达式:
(?:_)\K[0-9][0-9][0-9][0-9][0-9](?=_)
或如@MartinSmith建议(在评论中),您可以使用以下正则表达式:
_([\d]{5})_
在 SSIS 中实现正则表达式
- 首先添加另一个变量(例如:
@[User::FileNumber]
)
- 添加脚本任务并选择
@[User::Filename]
变量作为 ReadOnlyVariable,@[User:FileNumber]
作为 ReadWriteVariable
在脚本任务中使用以下代码:
using System.Text.RegularExpressions;
public void Main()
{
string strFilename = Dts.Variables["filename"].Value.ToString();
string strNumber;
var objRegEx = new Regex(@"(?:_)\K[0-9][0-9][0-9][0-9][0-9](?=_)");
var mc = objRegEx.Matches(strFilename);
//The last match contains the value needed
strNumber = mc[mc.Count - 1].Value;
Dts.Variables["FileNumber"].Value.ToString();
Dts.TaskResult = (int)ScriptResults.Success;
}
我正在将文件名拉入一个变量 (@[User::FileName]
) 并尝试从该字符串中提取工单编号(始终是 5 个数字,两边都有下划线)。例如,文件名看起来像 - "ABC_2017_DEF_9_12_GHI_35132_S5160.csv"
。我希望结果为 return“35132”。我找到了如何做到这一点的例子,例如 SUBSTRING(FileName,1,FINDSTRING(FileName,"_",1) - 1)
但下划线不会总是在同一个位置。
是否可以在表达式构建器中执行此操作?
答案:
public void Main()
{
string strFilename = Dts.Variables["User::FileName"].Value.ToString();
var RegexObj = new Regex(@"_([\d]{5})_");
var match = RegexObj.Match(strFilename);
if (match.Success)
{
Dts.Variables["User::WorkOrder"].Value = match.Groups[1].Value;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
我会使用脚本转换(如果不在 DataFlow 中则使用脚本任务)并使用正则表达式。
其他部分有什么意义吗?
无论如何你可以使用脚本任务和拆分功能。
以只读方式传入@fileName,以读写方式传入@WO
string fn = Dts.Variables["fileName"].Value;
string[] parts = fn.Split('_');
//Assuming it's always the 7th part
// You could extract the other parts as well.
Dts.Variables["WO"].Value = part(6);
首先,您提供的示例 ABC_2017_DEF_9_12_GHI_35132_S5160.csv
包含位于下划线之间的 4 个数字:
2017 , 9 , 12 , 35132
我不知道文件名是否包含很多个 5 位数字可以出现很多次,所以在我的回答中我会假设你想要 return 的数字是最后一次出现的数字由 5 个数字组成。
解决方案
您必须使用以下正则表达式:
(?:_)\K[0-9][0-9][0-9][0-9][0-9](?=_)
或如@MartinSmith建议(在评论中),您可以使用以下正则表达式:
_([\d]{5})_
在 SSIS 中实现正则表达式
- 首先添加另一个变量(例如:
@[User::FileNumber]
) - 添加脚本任务并选择
@[User::Filename]
变量作为 ReadOnlyVariable,@[User:FileNumber]
作为 ReadWriteVariable 在脚本任务中使用以下代码:
using System.Text.RegularExpressions; public void Main() { string strFilename = Dts.Variables["filename"].Value.ToString(); string strNumber; var objRegEx = new Regex(@"(?:_)\K[0-9][0-9][0-9][0-9][0-9](?=_)"); var mc = objRegEx.Matches(strFilename); //The last match contains the value needed strNumber = mc[mc.Count - 1].Value; Dts.Variables["FileNumber"].Value.ToString(); Dts.TaskResult = (int)ScriptResults.Success; }