VB 2010使用regex在filenpath字符串中提取客户名称

时间:2012-10-24 14:21:06

标签: regex vb.net string extract filepath

我得到了这段代码,用于从文件路径中提取作业号。我不知道如何从我发现的例子中正确使用正则表达式,但我希望现在能够只提取文件名的“blah_blah”部分。我很感激任何提示或建议。

示例文件路径:Q:\ 2463_Customer_Name .... \

我想为我的字符串获取'客户名称'。

Dim numRegex As New Regex("\d+") 
Dim number As String = numRegex.Match("Q:\2456_blah_blah\file.txt").Value 

谢谢,

1 个答案:

答案 0 :(得分:0)

我对VB.net中使用RegExes一无所知,但是像

这样的字符串
^[A-Z]:\\[0-9]{0,4}_(.+)\\.*$

应该提供一个很好的起点。反斜杠用反斜杠转义;)。究竟是什么决定了blah_blah部分的开始和结束?我想这是下划线和尾随\? 在我的正则表达式示例中,()包含blah_blah。

^ start of line
[A-Z] one upper case letter
:\\ followed by a colon and a backslash
[0-9]{0,4} followed by up to four digits between 0-9 (tweaking here might be necessary)
_ followed by an underscore
(.*) followed by at least one (+) arbitrary character
\\ followed by a backslash (again, escaped maybe you don't need this)
.*$ followed by any number of characters until the end of the line ($)

如果您对模式有了更多了解,那么您应该考虑将问题编辑得更清楚。是否只有blah_blah部分的信件,等等。

相关问题