On Mar 8, 12:45 am, "Branco Medeiros" <branco.medei...@gmail.com>
wrote:
Peter wrote:
<snip>So some of them appear as:
THH307A.Monitoring Public Health Issues.doc ' A single period
THH307A- Monitoring Public Health Issues.doc ' A hyphen, space
THH307A Monitoring Public Health Issues.doc ' A single space
THH307 A.Monitoring Public Health Issues.doc ' A mess
At the moment I can match filenames that have a period or space after
the prefix but can't work out how to also match a hyphen.
This is my regex at the moment:
Dim PrefixRegex As Regex = New Regex("(?<prefix>[^\.| ]+)[\.| ](?
<unitName>.+)")
<snip>
Maybe "(?<prefix>[^\.| ]+)[\.| ]+(?<unitName>.+)" will do. However,
the last example (with an embeded space in the prefix) will be more
challenging...
HTH.
Regards.
Branco.
Hey Branco,
Thanks for replying. I tried what you suggested but it appears to do
the same thing as my original regular expression. I am trying to
extract the prefix without the hyphen. But unfortunately using your
above mentioned regex the hyphen remains attached to the prefix.
I am using this small console app to test the regex:
Sub Main()
' My Regex:
' Dim PrefixRegex As Regex = New Regex("(?<prefix>[^\.| ]+)
[\.| ](?<unitName>.+)")
' Brancos Regex:
Dim PrefixRegex As Regex = New Regex("(?<prefix>[^\.| ]+)[\.| ]
+(?<unitName>.+)")
Dim filename As String = "THH307A Monitoring Public Health
Issues.doc"
Dim filename2 As String = "THH307A.Monitoring Public Health
Issues.doc"
Dim filename3 As String = "THH307A- Monitoring Public Health
Issues.doc "
Dim M As Match = PrefixRegex.Match(filename)
Dim M2 As Match = PrefixRegex.Match(filename2)
Dim M3 As Match = PrefixRegex.Match(filename3)
If M.Success Then
System.Console.WriteLine("Prefix: " &
M.Groups("prefix").Value)
System.Console.WriteLine("Unit Name: " &
M.Groups("unitName").Value)
Else
System.Console.WriteLine(filename & " is not a valid
filename")
End If
If M2.Success Then
System.Console.WriteLine("Prefix: " &
M2.Groups("prefix").Value)
System.Console.WriteLine("Unit Name: " &
M2.Groups("unitName").Value)
Else
System.Console.WriteLine(filename2 & " is not a valid
filename")
End If
If M3.Success Then
System.Console.WriteLine("Prefix: " &
M3.Groups("prefix").Value)
System.Console.WriteLine("Unit Name: " &
M3.Groups("unitName").Value)
Else
System.Console.WriteLine(filename3 & " is not a valid
filename")
End If
System.Console.WriteLine()
System.Console.WriteLine("Press Enter to Continue...")
System.Console.ReadLine()
End Sub
Output:
-----------
Prefix: THH307A
Unit Name: Monitoring Public Health Issues.doc
Prefix: THH307A
Unit Name: Monitoring Public Health Issues.doc
Prefix: THH307A-
Unit Name: Monitoring Public Health Issues.doc
Press Enter to Continue...
----------------------------------
Do you have any other ideas?
Thanks again,
Peter.