how do i replace exact phrases in c# string.replace -
I'm trying to make sure that a list of phrases is found by them and \ n + idiom Like
Your name: Your age: 28
My name: Your age: 28 I have a file with the phrases that I replace and through the bridge and the loop. Except for few words there are 2 words to use the I phrase, where the phrase starts and ends.
It does not seem to work, why do anyone know?
Example - The string is 'name: xxxxxx' is not edited.
output = output. ('\ B' + "name" + "b", 'match'); By using regular expressions, accounts for any number of words with spaces:
system. Text. Regular Expression; Reggae Rex = New Reggae ("(? & Lt; key & gt; \\ w + (\\ b \\ s + \\ w +) *) \\ s *: \\ s * (? & Lt; value & Gt; \\ w +) "); MatchCollection MC = re.Matches ("Your name: your age: 28"); Foretach (Match M in MC) {string key = m.Groups ("key") Values; String Value = M. Group ("value"). Values; / / Is stored in a list, but I will just write to the console. WrightLine (key + ":" + value); } Here's some explanation:
- Let's say what you want to the left side of the colon (:) with a key < / Strong>, and what's on the right - a value .
- Because these key / value pairs are at least different from space, due to this, there can be a word of value (otherwise we have ambiguity).
Uses groups with regular expression names so that the code can be made more readable.
Comments
Post a Comment