Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/astahost/public_html/rkrt/rkrt_tracker.php on line 186
Regular Expressions Not Matching
Astahost.com   Mar 22, 2010
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > BASIC / Visual Basic (.NET)

Regular Expressions Not Matching

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > BASIC / Visual Basic (.NET)

Regular Expressions Not Matching

Jimmy89
Hi All,
I am using Regular Expressions in VB.NET to find illegal filename characters in a string. The user enters and name, and it then saves the file with that name. I just need to make sure that the filename doesn't contain illegal filename characters.

The characters are / \ : ? " < > | (is there any others?)

I am using the following regex at the moment, but it doesn't seem to be picking up backslashes.

CODE
Dim myRegex As New System.Text.RegularExpressions.Regex("[\/:?'=<>|]")

Every other character it picks up, why just this one? Also the " character is illegal, how can i put that into the regex (at the moment it thinks its part of the code and not the regex)

If there is another way to do the same thing, or a different regex, that would be nice! tongue.gif

Thanks,
-jimmy

 

 

 


Comment/Reply (w/o sign-up)

dserban
Have you tried escaping the \ with another \ - therefore making a \\ out of it?
The same trick might work by putting a \" instead of the "
The backslash is a good guess for what the escape character is in many environments, it might be that in VB.NET it's the same as well.

Comment/Reply (w/o sign-up)

faulty.lee
For " you need to escape with " itself.
CODE
""



Comment/Reply (w/o sign-up)

yordan
And of course you probably need to protect \ from itself (usually \ means "ignore what follows me"). So, instead of \ you should write down \\.
I have this problem with the character $ in my script, I have to protect it, so instead of $ I write down \$.
So, amongs things not to be used in a filename, I think that you should add the following ones :
. (the dot, ".")
+ (because on Unix systems a+b will try to calculate the sum of a and b )
- (same reason, substracting)
=(a=b will be evaluated as "does the variable a be equal to the variable b" or a syntax error)
* (the star, means "everything" when interpreted by Unix or Linux)
space (the blank, " ") because "my file" will try to write down a file namde "my" and a file named "my"
$ (I guess, at least on IBM mainframe, but maybe your programs are not for mainframe ?)
And I'm sure that other forumers here will tell you a lot of other forbidden characters.
By the way, may I suggest you, if a blank or a minus is in the filename, replace it by _ (underscore) ? I see that other progs like 4image do it that way.

 

 

 


Comment/Reply (w/o sign-up)

Jimmy89
Thanks guys, those changes worked a treat! (I've never liked Regular Expressions, or maybe they haven't liked me) Yordan, thanks for the little extras - I've added them in also, you can never to too safe when it comes to end users and their inputs tongue.gif!

Cheers,
-jimmy

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Regular Expressions Matching


    Looking for regular, expressions, matching



See Also,

*SIMILAR VIDEOS*
Searching Video's for regular, expressions, matching
advertisement




Regular Expressions Not Matching

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com



Creative Commons License