how can i filter an arraylist with a regexp?

Posted on 2008-10-11
Medium Priority
Last Modified: 2012-06-21

i need a group of files out of a directory D,
i put all files names contained in the directory D  into an arraylist,

now i need to filter the arraylist as i need only to keep the files that begin with a certain
string like "0001".

files in arraylist,

i need to delete - in my arraylist - all the files that dont begin with a pattern of four numbers like here "0001",

in order to get

thank you very much for help,
i would be delighted with some code,
Question by:toshi_
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 600 total points
ID: 22696030
you cannot filter your array list with a regular expression

i will suggest that when you are creating the array list (filling it with file names) then why dont you skip addition of the files which you dont want in first place so that extra processing of removal of files later is saved
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 200 total points
ID: 22696400
why not do this instead? :

string[] filtered = System.IO.Directory.GetFiles("somepath", "001*.txt");

Author Comment

ID: 22709380

 sorry for the delay.....

I agree with you, thanks for the tip,
the thing is that i'm getting my files names through ftp with ftplib, and i dont have the possibility to
filter as you want me to filter,

this is how i recover the list of files,
if there's a possibility to do differently, without having all the files in the request , i would be happy,

   public ArrayList Get_FilesListFromFTP(string aPathToFTP, string anUser, string aPass)
            DomainLayer.FTP ftpLib = new FTP();
            ArrayList _fileNameList = new ArrayList();
                ftpLib.Connect(aPathToFTP.ToString(), anUser, aPass);
                _fileNameList = ftpLib.ListFiles();
            }catch(Exception ex)

            return _fileNameList;

thank you in advance,
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 600 total points
ID: 22709696
you can get files like DIR on the command prompt also
I am using DIR like for FTP as shown below
may be this will be good for you too
		public List<string> GetFileList ()
			List<string> downloadFiles = new List<string> ();
			FtpWebRequest reqFTP;
				reqFTP = (FtpWebRequest) FtpWebRequest.Create (new Uri ("ftp://" + m_UploadLocation + "/"));
				reqFTP.UseBinary = true;
				reqFTP.Credentials = new NetworkCredential (m_UserName, m_Password);
				reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
				WebResponse response = reqFTP.GetResponse ();
				StreamReader reader = new StreamReader (response.GetResponseStream ());
				string line = reader.ReadLine ();
				while (line != null)
					downloadFiles.Add (line);
					line = reader.ReadLine ();
				reader.Close ();
				response.Close ();
				return downloadFiles;
			catch (Exception ex)
				LogManager.WriteInformationLog (m_LoggingMessage.ToString ());
				m_LoggingMessage.Length = 0;
				LogManager.WriteInformationLog (ex.StackTrace);
				downloadFiles = new List<string> ();
				return downloadFiles;

Open in new window

LVL 23

Accepted Solution

Tony McCreath earned 1200 total points
ID: 22709734
Maybe this code will help.
public void Test()
    ArrayList list = new ArrayList();
    ArrayList newList = FilterArrayList(list, "^0001.*$");
    foreach (object o in newList)
public ArrayList FilterArrayList(ArrayList list, string regex)
    return FilterArrayList(list, new Regex(regex));
public ArrayList FilterArrayList(ArrayList list, Regex regex)
    ArrayList newList = new ArrayList();
    foreach (object o in list)
        if (o != null)
            if (regex.IsMatch(o.ToString()))
    return newList;

Open in new window


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question