This discussion is archived
4 Replies Latest reply: Dec 5, 2012 10:32 AM by DrClap RSS

Java regex for filenames or filetypes matching

881062 Newbie
Currently Being Moderated
I am trying to write a java regular expression where my application should restrict the names of the files or filetypes mentioned in the regex from being attached to an email. I started the regex like this,

.*\\.(?:jpeg|jpg)$


Is this correct?. Right now it is not working(matching). Not sure where I am going wrong. Also, how can I add new file types or filenames to this expression incase if I want to expand this regex. Like say, I want to add the vcf file types also here in this regex, how should I do that? Also, If I need to add the filenames, say abc.bmp from being attached, how and where should I include that filename in this regex?. Any help is greatly appreciated as I am new to this regex.
  • 1. Re: Java regex for filenames or filetypes matching
    DarrylBurke Guru Moderator
    Currently Being Moderated
    There's no need for Regex, and no need to reinvent a wheel that already exists. You can use javax.swing.filechooser.FileNameExtensionFilter and test the value returned from accept(...)

    db

    edit And if I'd seen that you don't bother to return to most of the threads you start, I wouldn't have bothered to respond to this one.

    Edited by: Darryl Burke
  • 2. Re: Java regex for filenames or filetypes matching
    881062 Newbie
    Currently Being Moderated
    Darryl , I can't use Swing in my application. So I need to use a regex. This regex will be in a configuration page in my application where the user(client) can modify it to notify the filetypes or file names that they want to ignore from processing. So once my application receives a file, it will get this regex and do a compare with the current file to check if it matches the pattern. If it matches, I should ignore the file. The current regex that I have is
    ^(?:.*\\.(?:all|allowed|file|extensions)|all\\.allowed|file\\.names)$

    Do you think if I am missing something here?. for some reason this doesn't work for me.
  • 3. Re: Java regex for filenames or filetypes matching
    FabianScherpenzeel Explorer
    Currently Being Moderated
    You do not need to use Swing. Try implementing: java.io.FilenameFilter
  • 4. Re: Java regex for filenames or filetypes matching
    DrClap Expert
    Currently Being Moderated
    878059 wrote:
    Darryl , I can't use Swing in my application.
    Does this mean "I can't use GUI components" or "I can't use any classes in the javax.swing hierarchy"?

    If it's the former (which would be the most reasonable interpretation in real life) then there's nothing stopping you from using javax.swing.filechooser.FileNameExtensionFilter.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points