Replace speacial characters with span tag using c# regular expressions.

Oct 27 2011 9:49 AM

We have a test like "This is a bug (solveing) ? & @ # 17066.".

After we modify the test with below logic :

// replace <br/> tags with new line characters
            string replaceContent = Regex.Replace(contentText, @"<br\s?/?>", "\n");

            // add full stop to inner text of an element, if it needs one, to separate from next word when tags removed
            replaceContent = Regex.Replace(replaceContent, @"\w(?=</\w+>)", @"$0\.");

            // remove all tags and leading and trailing space         
            replaceContent = Regex.Replace(replaceContent, "<.*?>", string.Empty).Trim();
            CommonMethods.WordMatchCollection = CommonMethods.WordRegularExpression.Matches(replaceContent);

            // match words which are not in tags
            string pattern = @"(?<!<[^>]*?)\b(\w+|\w+['-]\w+)\b(?![^<]*?>)";
            string replacement = "<span>$0</span>";

            // surround words in text with <span> tags
            contentText = Regex.Replace(contentText, pattern, replacement);

            return contentText;

The formated test is as follows:-

<SPAN id=0><span>This</span> <span>is</span> <span>a</span> <span>bug</span> (<span>solveing</span>) ? &<span>amp</span>; @ # <span>17066</span>.</SPAN><BR><BR><BR><SPAN id=1></SPAN>

We want to add Span tags for Speacial character like #, $, @ etc also.
How we can do this.

Thanks in Advanced.


Answers (3)