I need to replace the word PARAM_DATETIME in the string:
string input = "^FT734,274^A0I,28,28^FH\\^FDPARAM_DATETIME^FS";
I'm trying with:
string newstr = Regex.Replace("^FT734,274^A0I,28,28^FH\\^FDPARAM_DATETIME^FS", @"\bPARAM_DATETIME\b", "27-01-2022");
but it doesn´t work.
The goal is to match the word PARAM_DATETIME even if it is preceded with F at the start of the word followed with any uppercase letter.
CodePudding user response:
If you only need to change the word PARAM_DATETIME, isn't it easier to use String.Replace?
string input = "^FT734,274^A0I,28,28^FH\\^FDPARAM_DATETIME^FS";
input = input.Replace("PARAM_DATETIME", "27-01-2022");
CodePudding user response:
You can use
Regex.Replace(text, @"\b(F[A-Z])?PARAM_DATETIME\b", "${1}27-01-2022")
See the regex demo. Details:
\b- a word boundary(F[A-Z])?- Group 1 (optional):Fand then any one ASCII uppercase letterPARAM_DATETIME- a word\b- a word boundary
The match is replaced with Group 1 value (${1}) and the hardcoded string.
