Question

EDIT

@Ulugbek - I've edited to question to clarify.

I'm using a pattern as a delimiter and need to replace consecutive pattern occurrences by inserting a $ between the patterns using C#, but currently it only replaces the first instance of the pattern.

Is there something else I need to do other than run it through the replace several times? Or, do I need to use RegEx?

Thanks

String s = "ABCThis is a delimited valueABCABCABCAnd another delimited valueABC";
String.Replace("ABCABC", "ABC$ABC"); //misses second instance of the consecutive patterns instances 

Results:

ABCThis is a delimited valueABC$ABCABCAnd another delimited valueABC

Desired Results:

ABCThis is a delimited valueABC$ABC$ABCAnd another delimited valueABC

Ulugbek Solution:

ABC$This is a delimited valueABC$ABC$ABC$And another delimited valueABC$

Thanks

Was it helpful?

Solution

You can rethink the strategy. Replace all ABCs followed by ABC with ABC$.

string s = "ABCABCABC";
string output = Regex.Replace(s, "ABC(?=ABC)", "ABC$");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top