I have some string. I need a regex that will replace each occurrence of symbol that is not space + '<' with the same symbol + space + '<'.
In other words if there is '<' without ' ' before it it must add the space.
I've tried something like :
string pattern = "[^ ]<";
string replacement = "$0" + "<";
string result = Regex.Replace(html, pattern, replacement);
Obviously not working as I want.
string pattern = "([^ ])<";
string replacement = "$1" + " <";
You can try something like this.
Related
I want to empty the value between the hyphn for example need to clear the data in between the range of hyphen prefix and suffix then make it has empty string.
string templateContent = "Template content -macro- -UnitDetails- -testEmail- sending Successfully";
Output
templateContent = "Template content sending Successfully";
templateContent = Regex.Replace(templateContent, #"-\w*-\s?", string.Empty).TrimEnd(' ');
#"-\w*-\s" - is regex pattern for '-Word- '
- - pattern for -
\w - word character.
* - zero or any occurrences of \w
\s - pattern for whitespace character
? - marks \s as optional
TrimEnd(' ') - to remove trailing space if there was a pattern at end of the string
There are many ways to do this, however given your example the following should work
var split = templateContent
.Split(' ')
.Where(x => !x.StartsWith("-") && !x.EndsWith("-"));
var result = string.Join(" ",split);
Console.WriteLine(result);
Output
Template content sending Successfully
Full Demo Here
Note : I personally think regex is better suited to this
You can use regex for this
string regExp = "(-[a-zA-Z]*-)";
string tmp = Regex.Replace(templateContent , regExp, "");
string finalStr = Regex.Replace(tmp, " {2,}", " ");
var resultWithSpaces = Regex.Replace(templateContent, #"-\S+-", string.Empty);
This regular expression looks for two hyphens surrounding one or more characters that are not white space.
It will leave the spaces that were around the removed word. To get rid of those you can do another Regex to replace multiple spaces with a single space.
var result = Regex.Replace(resultWithSpaces, #"\s+", " ");
I want to dynamically adjust my replacement pattern and evaluator:
string pattern = "np";
string replacement = "ab";
string retval = Regex.Replace("Input", #"(.*)" + pattern + #"(.*)", #"$1" + replacement + #"$2";
// retval = "Iabut" => correct
string replacement = "12";
retval = Regex.Replace("Input", #"(.*)" + pattern + #"(.*)", #"$1" + replacement + #"$2";
// retval = "$112ut" => wrong
The problem is that in the second case my evaluator is "$112$2" so my first replacement group would be $112.
Is it possible to avoid such problems directly or do I need to put a delimiting character between my group definition and my string?
As a replacement argument, use
"${1}" + replacement.Replace("$", "$$") + "$2"
The braces in ${1} will make sure the first group is referred to and .Replace("$", "$$") will make it work well if the replacement has $ inside.
My code is as follow:
ArticleContent = Regex.Replace(_article.Article, "[QUOTE]", "<p class='quote'><span style='font-size:1.8em !important;'>" + _article.NewFields.Quotes + "</span></p>", RegexOptions.IgnoreCase);
The problem i'm facing here is, the Regex is not replacing the whole occurrence of the string '[QUOTE]'. Instead it is searching for the letters q,u,o,t,e and replacing them with the replace string. I know the issue is because of the square brackets, but i want that to be replaced as well. Please help.
You must escape square brackets! And don't forget REGEXP are case sensitive. Here's my correction to your code:
ArticleContent = Regex.Replace(_article.Article, "\[quote\]", "<p class='quote'><span style='font-size:1.8em !important;'>" + _article.NewFields.Quotes + "</span></p>", RegexOptions.IgnoreCase);
By the way, I don't see any accourrence of 'quote' enclosed in brackets, so I'm not sure I got what you're trying to do...
Use an non capturing group to replace all the occurrences of the string QUOTE with your desired string ,
(?:QUOTE)
So your code should be,
ArticleContent = Regex.Replace(_article.Article, "(?:QUOTE)", "<p class='quote'><span style='font-size:1.8em !important;'>" + _article.NewFields.Quotes + "</span></p>", RegexOptions.IgnoreCase);
OR
Try to escape the square brackets, if you want to replace [QUOTE] with some-other string becuase suare brackets in regex have a special meaning.
\[QUOTE\]
And your code should be,
ArticleContent = Regex.Replace(_article.Article, "\[QUOTE\]", "<p class='quote'><span style='font-size:1.8em !important;'>" + _article.NewFields.Quotes + "</span></p>", RegexOptions.IgnoreCase);
I have a string in which I want to replace a whole word. This is what I have:
var TheWord = "SomeWord";
TheWord = "\b" + TheWord + "\b";
TheText = TheText.replace(TheWord, "SomeOtherWord");
I'm using "\b" because I only want to replace "SomeWord", not "SomeWordDifferent". The text looks like this: var TheHTML = '<div class="SomeWord">'; However, the replacement doesn't take place. What do I need to change?
You need to escape the backslashes. Try either of these...
TheWord = #"\b" + TheWord + #"\b";
or
TheWord = "\\b" + TheWord + "\\b";
I assume you are trying to use Regex. The method for this is
string Regex.Replace(string input, string replacment)
So I think this is what you want:
string text = ...; // text comes from somewhere
string pattern = #"\bSomeWord\b"; // escape \b (word boundary regex anchor), or use verbatim string literal, like here
var regex = new Regex(pattern);
text = regex.Replace(text, "SomeOtherWord");
Or simply the static version of Replace method as Tim wrote:
Regex.Replace(text, pattern, "SomeOtherWord");
I am not clued up on Regex as much as I should be, so this may seem like a silly question.
I am splitting a string into a string[] with .Split(' ').
The purpose is to check the words, or replace any.
The problem I'm having now, is that for the word to be replaces, it has to be an exact match, but with the way I'm splitting it, there might be a ( or [ with the split word.
So far, to counter that, I'm using something like this:
formattedText.Replace(">", "> ").Replace("<", " <").Split(' ').
This works fine for now, but I want to incorporate more special chars, such as [;\\/:*?\"<>|&'].
Is there a quicker way than the method of my replacing, such as Regex? I have a feeling my route is far from the best answer.
EDIT
This is an (example) string
would be replaced to
This is an ( example ) string
If you want to replace whole words, you can do that with a regular expression like this.
string text = "This is an example (example) noexample";
string newText = Regex.Replace(text, #"\bexample\b", "!foo!");
newText will contain "This an !foo! (!foo!) noexample"
The key here is that the \b is the word break metacharacter. So it will match at the beginning or end of a line, and the transitions between word characters (\w) and non-word characters (\W). The biggest difference between it and using \w or \W is that those won't match at the beginning or end of lines.
I thing this is the right thing you want
if you want these -> ;\/:*?"<>|&' symbols to replace
string input = "(exam;\\/:*?\"<>|&'ple)";
Regex reg = new Regex("[;\\/:*?\"<>|&']");
string result = reg.Replace(input, delegate(Match m)
{
return " " + m.Value + " ";
});
if you want to replace all characters except a-zA-Z0-9_
string input = "(example)";
Regex reg = new Regex(#"\W");
string result = reg.Replace(input, delegate(Match m)
{
return " " + m.Value + " ";
});