Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Regular Expressions Not Matching
Jimmy89
post Feb 20 2008, 03:44 AM
Post #1


Living at the Datacenter
Group Icon

Group: [HOSTED]
Posts: 696
Joined: 30-June 06
From: Australia
Member No.: 14,219



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
Go to the top of the page
 
+Quote Post
dserban
post Feb 20 2008, 07:25 AM
Post #2


Premium Member
Group Icon

Group: [HOSTED]
Posts: 286
Joined: 17-June 07
Member No.: 22,702



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.

This post has been edited by dserban: Feb 20 2008, 07:27 AM
Go to the top of the page
 
+Quote Post
faulty.lee
post Feb 20 2008, 09:49 AM
Post #3


Premium Member
Group Icon

Group: [HOSTED]
Posts: 479
Joined: 5-November 06
Member No.: 17,016



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


Go to the top of the page
 
+Quote Post
yordan
post Feb 20 2008, 07:10 PM
Post #4


Way Out Of Control - You need a life :)
Group Icon

Group: [MODERATOR]
Posts: 1,980
Joined: 16-August 05
Member No.: 7,896



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.
Go to the top of the page
 
+Quote Post
Jimmy89
post Feb 21 2008, 03:24 AM
Post #5


Living at the Datacenter
Group Icon

Group: [HOSTED]
Posts: 696
Joined: 30-June 06
From: Australia
Member No.: 14,219



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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Request To Upgrade(1)
  2. Matching Sound To Animations In Flash(2)
  3. Regular Used Terms In The Webhosting World(4)
  4. Php Regular Expressions(11)
  5. Matching Text In A MySQL Database(3)
  6. Megatsunamis(9)
  7. Difference Between Starter And Regular(3)
  8. Airtel GPRS(22)
  9. Any Active & Regular Vietnamese Member At Astahost ?(3)
  10. Announcement: Importance Of Regular Site Backups !(18)
  11. How To Remove Query String Using Regular Expressions(4)
  12. The Absolute Bare Minimum Every Programmer Should Know About Regular Expressions(1)
  13. Automated Product Suggestion Script(2)
  14. Filtering Out Unwanted Junk Mail Using Regular Expression.(0)
  15. Using Regular Expressions To Parse Functions(5)
  1. Regular Expressions(6)


 



- Lo-Fi Version Time is now: 8th September 2008 - 06:31 AM