Question

I have text file with a row like this:

SendersTimeSeriesIdentification: COMPANY_A/COMPANY_B/REF_7/20090505

I'd like to replace all non-word chars in the value part with the character n, like this:

SendersTimeSeriesIdentification: COMPANYnAnCOMPANYnBnREFn7n20090505

But there are similar strings all over the file, which must remain intact. An example being:

MessageIdentification: REF_7/VER_1/20090505

I think I must use lookbehind, and I came to this attempt (VB.NET):

Regex.Replace(fileContentString, "(?<=SendersTimeSeriesIdentification: )(\W)", "0")

This doesn't work as I'd like it to. So my questions are:
Is it possible to replace all non-word characters in a specific piece of string with just one Regex.Replace call? How?

Was it helpful?

Solution

Try this one:

Regex.Replace(fileContentString, "(?<=SendersTimeSeriesIdentification:\s.*)[_\W]", "0")

This replaces all \W and _ chars with "0" after "SendersTimeSeriesIdentification: ".

OTHER TIPS

Rather than doing as a single regex replace, I'd split the file into lines, then only process lines that start with "SendersTimeSeriesIdentification: ". That way the regex replacement is nice and simple.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top