I have some code as follows:
string str = txtHTML2.Text;
int start = str.IndexOf("first") /*+ "first".Length*/;
int finish = str.LastIndexOf("last");
string str2 = str.Substring(start,finish - start);
listBox1.Items.Add(str2);
This copys the first ocurance between first\last to a listbox, what I need to do is to iterate through the whole textbox to the end because there is more to cut
Is there a way to do this? am I on the right path
any pointer to articales would be great
thanks in advance
Mike
VulpesRank #55 · 29.2K reputationPosted Apr 18, 2012, 6:46 AM EDT
The first and the last of the month
then your code would extract the following string (followed by a space) and add it to the ListBox:
first and the
If you had multiple occurrences of 'first' and 'last' in the same string, then the easiest way to extract all of them would be using a regular expression:
using System.Text.RegularExpressions;
// ...
string str = txtHTML2.Text;
MatchCollection matches = Regex.Matches(str, @"first.*?(?=last)");
foreach(Match m in matches) listBox1.Items.Add(m.Value);
MIke Lumley16 reputationPosted Apr 18, 2012, 8:06 AM EDT
Hi Vulpes thanks agian for a speedy reply
I've tried your code, runs with no errors but nothing goes in listbox.
When I set breakpoint on first line, the "txtHTML" has text then "str" has same value,
but looking at "matches" stays with a value of "null" even on the exit of the method.
I've gone back to my original code and sent string to a textbox and it get nearly all the
original HTML. I've used this to get narrow down the block of HTML that I need, so getting there slowly.
int start = str.IndexOf("
int finish = str.LastIndexOf("£");
string str2 = str.Substring(start, finish - start);
textBox1.Text = str2;
Just been looking at Regex wow what a topic.
Nice explanation of i++
Thanks Vulpes.
Mike
VulpesRank #55 · 29.2K reputationPosted Apr 18, 2012, 8:56 AM EDT
MIke Lumley16 reputationPosted Apr 18, 2012, 9:22 AM EDT
That WORKED a treat
Thankyou
Thankyou
Thankyou
Thankyou
Words cannot express how much!!
YOU are 1 TOP Coder
I've got to study this Regex because I'm going to need more can point me in the best plase to start
Thankyou once again Vulpes
Mike
VulpesRank #55 · 29.2K reputationPosted Apr 18, 2012, 9:35 AM EDT
http://www.codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts