• Hello and welcome to our new forums. We upgraded our forum sites to a more robust and modern system which we hope you will enjoy. Be sure to check out your profile by clicking the button on the top right and configure your preferences, signature, time zone, avatar, etc. as you wish. If you need help with using this new forum'ware try the help link on the bottom right.

    Click here to review your account now.

Using Regex

Joined
Jan 23, 2011
Messages
3
Programming Experience
10+
Private Shared Function MakeValidFileName(ByVal name As String) As String
Dim invalidChars As String = System.Text.RegularExpressions.Regex.Escape(New String(System.IO.Path.GetInvalidFileNameChars))
Dim invalidRegStr As String = String.Format("([{0}]*\.+$)|([{0}]+)", invalidChars)
Return System.Text.RegularExpressions.Regex.Replace(name, invalidRegStr, "_")
End Function

Can you tell me what the line starting with invalidRegStr does?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,254
Location
Norway
Programming Experience
10+
Joined
Jan 23, 2011
Messages
3
Programming Experience
10+
I should of said in my post, I know what the {0} do but:

I don't see why two of them and don't know what the other characters do.

Couldn't they all be added to invalidChars ?

Thanks
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,254
Location
Norway
Programming Experience
10+
I don't see why two of them
The arguments can be added multiple places in the string.
and don't know what the other characters do
That is the regex expression, how sound that expression is you can decide yourself.
Lets say you take one illegal char \ and escape it \\ and put that in the placeholders, then you get this string/expression ([\\]*\.+$)|([\\]+)
Now you can copy and paste that into the expression box here Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript and you will get this explanation:
4446

You can also test input string the expression there.
If you want to learn regex you can for example go here: Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,254
Location
Norway
Programming Experience
10+
In top-right menu you can 'edit thread' and set a resolved/answered thread prefix.
 
Top Bottom