Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to replace second occurrence of Sub string in my main string with particular pattern using Regex.Replace() Method.

Ex.
HTML
<table>
<tr>
<td></td>
</tr>
</table>
 
<table>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>


I have above string with two table tags. I want replace second occurrence of table tag
HTML
<table>
with
HTML
<table id="mytable1">
.
How can i replace only second occurrence of table with my pattern. ?
Posted
Updated 29-Oct-15 1:25am
v4

You can use it
C#
public string Replace2ndOccurence(string inputStr, string valueToReplace2ndOccurance)
   {
       Match m = Regex.Match(inputStr, "((" + Regex.Escape(valueToReplace2ndOccurance) + ").*?){2}");
       int index = 0;
       if (m.Success)
           index = m.Groups[2].Captures[1].Index;
       return inputStr.Remove(index, valueToReplace2ndOccurance.Length);
   }
 
Share this answer
 
Comments
[no name] 29-Oct-15 7:52am    
did it Solved your problem ?
Veeshal Mali 31-Oct-15 2:47am    
No not yet....
its not replacing any string belongs to my string.
[no name] 31-Oct-15 3:02am    
Its working on your posted string also on our end. Can you please post your string on which its not working?
Veeshal Mali 31-Oct-15 3:05am    
No pankaj sir... Your solution doesn't solve my problem... Please give me best solution sir....
[no name] 31-Oct-15 3:13am    
Can you please post your test string ?
Maybe you can use XML instead of Regex:
C#
var doc = XDocument.Parse(@"<root><table>
<tr>
<td></td>
</tr>
</table>
 
<table>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table></root>");

var secondTable = doc.Root.Elements("table").Skip(1).First();
secondTable.SetAttributeValue("id", "mytable1");

var replaced = doc.ToString();
 
Share this answer
 
Comments
Maciej Los 29-Oct-15 8:14am    
Very interesting solution, my 5!
Sergey Alexandrovich Kryukov 29-Oct-15 11:16am    
Indeed, why using Regex on XML? A 5.
—SA
Use this solution to replace the string
C#
public string Replace2ndOccurence(string inputStr, string valueToReplace2ndOccurance,string newString)
   {
       Match m = Regex.Match(inputStr, "((" + Regex.Escape(valueToReplace2ndOccurance) + ").*?){2}");
       int index = 0;
       if (m.Success)
           index = m.Groups[2].Captures[1].Index;
       inputStr = inputStr.Remove(index, valueToReplace2ndOccurance.Length);
       return inputStr.Insert(index, newString);
   }
 
Share this answer
 
Comments
Veeshal Mali 31-Oct-15 4:38am    
thank you sir...
its working....
Veeshal Mali 31-Oct-15 4:39am    
Sir Have a one more query...
Please give me a solution...
After Second table i have one div in which there is one paragraph... I want to remove this whole division after this table...
[no name] 31-Oct-15 4:44am    
Can you please post your full string ?
Veeshal Mali 31-Oct-15 5:42am    
<table><tr><td></td><td>2015-10-30 21:00:43 GMT</td></tr></table></div><table><tr><td>Symbol</td><td>Last</td><td>5 Minutes</td><td>15 Minutes</td><td>Hourly</td><td>Daily</td></tr><tr><td><span class="ftqa11bb" style="color:#0059B0;">EUR/USD</td><td><span class="ftqa11bbb" style="color:#d80100;">1.1004</td><td>Buy</td><td>Neutral</td><td>Strong Sell</td><td>Strong Sell</td></tr><tr><td><span class="ftqa11bb" style="color:#0059B0;">GBP/USD</td><td><span class="ftqa11bbb" style="color:#05a007;">1.5427</td><td>Strong Buy</td><td>Neutral</td><td>Strong Buy</td><td>Buy</td></tr><tr><td><span class="ftqa11bb" style="color:#0059B0;">USD/JPY</td><td><span class="ftqa11bbb" style="color:#d80100;">120.62</td><td>Strong Sell</td><td>Strong Sell</td><td>Sell</td><td>Neutral</td></tr><tr><td><span class="ftqa11bb" style="color:#0059B0;">USD/CHF</td><td><span class="ftqa11bbb" style="color:#05a007;">0.9880</td><td>Neutral</td><td>Neutral</td><td>Neutral</td><td>Strong Buy</td></tr><tr><td><span class="ftqa11bb" style="color:#0059B0;">AUD/USD</td><td><span class="ftqa11bbb" style="color:#757575;">0.7136</td><td>Strong Buy</td><td>Strong Buy</td><td>Strong Buy</td><td>Strong Sell</td></tr><tr><td><span class="ftqa11bb" style="color:#0059B0;">EUR/GBP</td><td><span class="ftqa11bbb" style="color:#d80100;">0.7136</td><td>Strong Buy</td><td>Neutral</td><td>Strong Sell</td><td>Strong Sell</td></tr></tbody></table>
<div>Fusion Media/Investing.com will not accept any liability for loss or damage as a result of reliance on the information contained within this tool including data, quotes, charts and buy/sell signals. Please be fully informed regarding the risks and costs associated with trading the financial markets, it is one of the riskiest investment forms possible. Currency trading on margin involves high risk, and is not suitable for all investors. Before deciding to trade foreign exchange or any other financial instrument you should carefully consider your investment objectives, level of experience, and risk appetite.</div>

After second there is a one div with some content. Now I want to remove this whole siv with content too. only keep this both tables.
[no name] 31-Oct-15 5:49am    
You Can do that. Can you create a new question for that? i will give solution on that.

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