Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
This below is a code that extracts everything from listBox1 after "sometext" in textBox1.text, and it works well.

private void extract()
        {
            string s = string.Join(Environment.NewLine, listBox1.SelectedItems.OfType<object>().Select(i => Regex.Match(i.ToString(), @"sametext?(.*?)(?=#|$)").Groups[1].Value));
            textBox1.Text = (s.ToString());
        }

"sometext" is not always the same.


I would like to replace "sametext" with textBox2.text

textBox2.Text = different text

I tried:


What I have tried:

private void extract()
        {
            string s = string.Join(Environment.NewLine, listBox1.SelectedItems.OfType<object>().Select(i => Regex.Match(i.ToString(), "@"+ textBox2.Text + "?(.*?)(?=#|$)").Groups[1].Value));
            textBox1.Text = (s.ToString());
        }

But it does not work.

Please help me.

Thank you.
Posted
Updated 12-Jun-18 5:30am

Put your "pattern" in a separate string (before using it in "Match"), display it, and observe "why" it is not what you expect.

You're making a lot of assumptions about which TextBox contains what, etc. when you have no "proof".
 
Share this answer
 
Comments
Member 10410972 11-Jun-18 14:29pm    
Thanks Gerry, but I do not know how to do it...
With C#, "" and @"" are 2 different kind of strings: see documentation for details.
Your problem is that '@' is not part of the string.
C#
// This
@"sametext?(.*?)(?=#|$)"
// do not translate to this
"@"+ textBox2.Text + "?(.*?)(?=#|$)"
// but to this
textBox2.Text + @"?(.*?)(?=#|$)"
 
Share this answer
 
Comments
Member 10410972 12-Jun-18 11:20am    
Thanks ppolymorphe, the problem is solved.
Assuming you don't want the user to enter a regular expression, you'll need to "escape" the text they've entered.

You'll also want to limit your search to items which actually match the specified text.

I've also assumed you want to match a literal question-mark after the entered text, rather than making the last character of the entered text optional.
C#
string pattern = Regex.Escape(textBox2.Text) + @"\?(.*?)(?=\#|$)";

var matchingItems = listBox1.SelectedItems.OfType<object>()
    .Select(i => i.ToString())
    .Select(s => Regex.Match(s, pattern))
    .Where(m => m.Success)
    .Select(m => m.Groups[1].Value);

textBox1.Text = string.Join(Environment.NewLine, matchingItems);

NB: You should do yourself a favour, and stop accepting Visual Studio's default names for everything. Give your controls meaningful names - you'll thank yourself in six months when you come back to edit your code, and you don't have to spend ages working out what each control is supposed to represent! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900