Solved

how can i filter an arraylist with a regexp?

Posted on 2008-10-11
5
1,711 Views
Last Modified: 2012-06-21
hello,

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,
0001122.txt
0001123.txt
1001234.txt
......

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
0001122.txt
0001123.txt.


thank you very much for help,
i would be delighted with some code,
0
Comment
Question by:toshi_
5 Comments
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 150 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
0
 
LVL 55

Assisted Solution

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

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

Author Comment

by:toshi_
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();
            try
            {
                ftpLib.Connect(aPathToFTP.ToString(), anUser, aPass);
                _fileNameList = ftpLib.ListFiles();
            }catch(Exception ex)
            {
                Console.Write(ex.Message);
            }

            return _fileNameList;
        }


thank you in advance,
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 150 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;
			try
			{
				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

0
 
LVL 23

Accepted Solution

by:
Tiggerito earned 300 total points
ID: 22709734
Maybe this code will help.
public void Test()
{
    ArrayList list = new ArrayList();
 
    list.Add("0001122.txt");
    list.Add("0001123.txt");
    list.Add("1001234.txt");
 
    ArrayList newList = FilterArrayList(list, "^0001.*$");
 
    foreach (object o in newList)
    {
        Debug.WriteLine(o.ToString());
    }
}
 
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()))
                newList.Add(o);
        }
    }
    return newList;
}

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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