Jump to content

Regex Match para ListBox


Tatia
 Share

Recommended Posts

Boas amigos,

Estou a tentar encontrar uns valores em ficheiro de texto e apresentar numa listbox, porem ele s贸 apresenta o primeiro valor encontrado.

Actualmente para teste tenho o seguinte em um ficheiro de texto:

-rw-------	 12354 /Pasta/data/arquivo.rar
-rw-------	 265584 /Pasta/data2/arquivo.rar
-rw-------	 262160 /Pasta/data3/data3/arquivo.zip

E eu pretendo apenas que seja apresentado na listbox o seguinte:

/Pasta/data/arquivo.rar
/Pasta/data2/arquivo.rar
/Pasta/data3/data3/arquivo.zip

Meu c贸digo de teste utilizado esta assim:

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace Teste1
{
public partial class Form1 : Form
{
	public Form1()
	{
		InitializeComponent();
	}

	private void button1_Click(object sender, EventArgs e)
	{
		string Texto = File.ReadAllText("out.txt");
		Match match = Regex.Match(Texto, @"/Pasta/([\w-\d]*.*)", RegexOptions.Multiline);

		if (match.Success)
		{
			listBox1.Items.Add(match.Groups[0].Value);  
		}		
	}
	private void button2_Click(object sender, EventArgs e)
	{
		listBox1.Items.Clear();
	}
}
}

--Edite--

Tentei algo assim que acho que seria o mais correcto mas sem sucesso. 馃槥

string input = File.ReadAllText("out.txt");

	    string pattern = @"/Pasta/([\w-\d]*.*)";

	    MatchCollection matches = Regex.Matches(input, pattern);

	    foreach (Match match in matches)
	    {
		    try
		    {
			    listBox1.Items.Add(match.Groups[0].Value);
		    }
		    catch { }
	    }
Edited by Tatia
Link to comment
Share on other sites

Falta-te fechar uma chaveta, a menos que n茫o tenhas copiado todo o c贸digo correctamente. De resto tamb茅m funciona.

O exemplo que te apresentei foi aquele porque era o que eu j谩 tinha a m茫o fazendo a substitui莽茫o do devido pattern e porque n茫o tinha oportunidade de testar o teu c贸digo.

Ate tinha a chaveta, mas n茫o dava...

Bom agora esta resolvido muito obrigado...

Edite:

Ja agora ap贸s ter na listbox o valor seguinte:

/Pasta/data/arquivo.rar

Como obtenho apenas o nome e extens茫o do ficheiro?

Neste caso seria.

arquivo.rar

O que estou a fazer e seleccionar o ficheiro na listbox e guardar o arquivo em outro local.

Ja esta tudo a trabalhar so que tenho de ter o nome do ficheiro definido, pretendo usar o nome que e apresentado na listbox para guardar o ficheiro...

Um abra莽o

Edited by Tatia
Link to comment
Share on other sites

string nome = System.IO.Path.GetFileName(match.Value);
string nomeSemExt = System.IO.Path.GetFileNameWithoutExtension(match.Value);
string ext = System.IO.Path.GetExtension(match.Value);

Podes colocar um exemplo de como usar seguindo o teu c贸digo?

var matches = Regex.Matches(Texto, @"/Pasta/([\w-\d]*.*)", RegexOptions.Multiline);
foreach (Match m in matches)
{
  listBox1.Items.Add(m.Value);
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.