C# Regex replace with Regex - c#

I have a problem, I want to replace every "[[:de:<text>]]" with "{{de|<text>}}" in some text. I tried with
output = Regex.Replace(input, "[[:de:(.*)]]", "{{de|(.*)}}");
but it doesnt copy the <text>.
I have no other idea how to replace this correctly.
Hope you can help me.

Use a lazy dot pattern and backreferences and escape the [ symbols:
output = Regex.Replace(input, #"\[\[:de:(.*?)]]", "{{de|$1}}");
If the text between de: and ]] can contain line breaks, use RegexOptions.Singleline modifier.
See the regex demo.

If you encapsulate everything inside groups, you can benefit of a MatchEvaluator. Try it online.
public static void Main()
{
var input = "[[:de:Hello World]]";
var pattern = #"(\[\[:de:)(.+)(\]\])";
var output = Regex.Replace(input, pattern, m => "{{de|" + m.Groups[2].Value + "}}");
Console.WriteLine(output);
}
output
{{de|Hello World}}

Do you really need regex ? I think you can only use string replace method;
output = input.Replace("[[:de:(.*)]]", "{{de|(.*)}}");

Related

How can I split a regex into exact words?

I need a little help regarding Regular Expressions in C#
I have the following string
"[[Sender.Name]]\r[[Sender.AdditionalInfo]]\r[[Sender.Street]]\r[[Sender.ZipCode]] [[Sender.Location]]\r[[Sender.Country]]\r"
The string could also contain spaces and theoretically any other characters. So I really need do match the [[words]].
What I need is a text array like this
"[[Sender.Name]]",
"[[Sender.AdditionalInfo]]",
"[[Sender.Street]]",
// ... And so on.
I'm pretty sure that this is perfectly doable with:
var stringArray = Regex.Split(line, #"\[\[+\]\]")
I'm just too stupid to find the correct Regex for the Regex.Split() call.
Anyone here that can tell me the correct Regular Expression to use in my case?
As you can tell I'm not that experienced with RegEx :)
Why dont you split according to "\r"?
and you dont need regex for that just use the standard string function
string[] delimiters = {#"\r"};
string[] split = line.Split(delimiters,StringSplitOptions.None);
Do matching if you want to get the [[..]] block.
Regex rgx = new Regex(#"\[\[.*?\]\]");
foreach (Match m in rgx.Matches(input))
Console.WriteLine(m.Groups[0].Value);
IDEONE
The regex you are using (\[\[+\]\]) will capture: literal [s 2 or more, then 2 literal ]s.
A regex solution is capturing all the non-[s inside doubled [ and ]s (and the string inside the brackets should not be empty, I guess?), and cast MatchCollection to a list or array (here is an example with a list):
var str = "[[Sender.Name]]\r[[Sender.AdditionalInfo]]\r[[Sender.Street]]\r[[Sender.ZipCode]] [[Sender.Location]]\r[[Sender.Country]]\r";
var rgx22 = new Regex(#"\[\[[^]]+?\]\]");
var res345 = rgx22.Matches(str).Cast<Match>().ToList();
Output:

C# regual expression needed

I have a bunch of text and in that text I would like to replace every "EXAMPLE" word that does NOT contain a a-z or A-Z character in front of it.. so something like "[^a-z]EXAMPLE"..
But when deleting I just want to delete the "EXAMPLE", not the misc character in front of it or any characters behind it...
So in "BLABLAEXAMPLBLA EXAMPLEBLA" i want to output "BLABLAEXAMPLBLA BLA"
I hope this is preety clear :)
Thank you for your time!
You can achieve this using negative lookbehind:
string cleanString = Regex.Replace(originalString, "(?<![a-zA-Z])EXAMPLE", "");
You can also use match evaluator. I think more elastic but more complicated too...
var regex = new Regex("[^a-z](?<a>EXAMPLE)");
var text = "BLABLAEXAMPLBLA EXAMPLEBLA";
MatchEvaluator evaluator = RemoveExample;
text = regex.Replace(text, evaluator);
...
private static string RemoveExample(Match m)
{
return m.Value.Replace(m.Groups["a"].Value, "");
}

Can I use regular expressions with String.Replace in C#?

For example I have code below
string txt="I have strings like West, and West; and west, and Western."
I would like to replace the word west or West with some other word. But I would like not to replace West in Western.
Can I use regular expression in string.replace? I used
inputText.Replace("(\\sWest.\\s)",temp); It dos not work.
No, but you can use the Regex class.
Code to replace the whole word (rather than part of the word):
string s = "Go west Life is peaceful there";
s = Regex.Replace(s, #"\bwest\b", "something");
Answer to the question is NO - you cannot use regexp in string.Replace.
If you want to use a regular expression, you must use the Regex class, as everyone stated in their answers.
Have you looked at Regex.Replace? Also, be sure to catch the return value; Replace (via any string mechanism) returns a new string - it doesn't do an in-place replace.
Try using the System.Text.RegularExpressions.Regex class. It has a static Replace method. I'm not good with regular expressions, but something like
string outputText = Regex.Replace(inputText, "(\\sWest.\\s)", temp);
should work, if your regular expression is correct.
Insert the regular expression in the code before class
using System.Text.RegularExpressions;
below is the code for string replace using regex
string input = "Dot > Not Perls";
// Use Regex.Replace to replace the pattern in the input.
string output = Regex.Replace(input, "some string", ">");
source : http://www.dotnetperls.com/regex-replace
USe this code if you want it to be case insensitive
string pattern = #"\bwest\b";
string modifiedString = Regex.Replace(input, pattern, strReplacement, RegexOptions.IgnoreCase);
In Java, String#replace accepts strings in regex format but C# can do this as well using extensions:
public static string ReplaceX(this string text, string regex, string replacement) {
return Regex.Replace(text, regex, replacement);
}
And use it like:
var text = " space more spaces ";
text.Trim().ReplaceX(#"\s+", " "); // "space more spaces"
I agree with Robert Harvey's solution except for one small modification:
s = Regex.Replace(s, #"\bwest\b", "something", RegexOptions.IgnoreCase);
This will replace both "West" and "west" with your new word

How to take only first line from the multiline text

How can I get only the first line of multiline text using regular expressions?
string test = #"just take this first line
even there is
some more
lines here";
Match m = Regex.Match(test, "^", RegexOptions.Multiline);
if (m.Success)
Console.Write(m.Groups[0].Value);
If you just need the first line, you can do it without using a regex like this
var firstline = test.Substring(0, test.IndexOf(Environment.NewLine));
As much as I like regexs, you don't really need them for everything, so unless this is part of some larger regex exercise, I would go for the simpler solution in this case.
string test = #"just take this first line
even there is
some more
lines here";
Match m = Regex.Match(test, "^(.*)", RegexOptions.Multiline);
if (m.Success)
Console.Write(m.Groups[0].Value);
. is often touted to match any character, while this isn't totally true. . matches any character only if you use the RegexOptions.Singleline option. Without this option, it matches any character except for '\n' (end of line).
That said, a better option is likely to be:
string test = #"just take this first line
even there is
some more
lines here";
string firstLine = test.Split(new string[] {Environment.NewLine}, StringSplitOptions.None)[0];
And better yet, is Brian Rasmussen's version:
string firstline = test.Substring(0, test.IndexOf(Environment.NewLine));
Try this one:
Match m = Regex.Match(test, #".*\n", RegexOptions.Multiline);
This kind of line replaces rest of text after linefeed with empty string.
test = Regex.Replace(test, "(\n.*)$", "", RegexOptions.Singleline);
This will work also properly if string does not have linefeed - then no replacement will be done.

Remove characters using Regex

I have a string. I need to replace all instances of a given array of strings from this original string - how would I do that?
Currently I am using...
var inputString = "this is my original string.";
var replacement = "";
var pattern = string.Join("|", arrayOfStringsToRemove);
Regex.Replace(inputString, pattern, replacement);
This works fine, but unfortunately it breaks down when someone tries to remove a character that has a special meaning in the regex.
How should I do this? Is there a better way?
Build the pattern using Regex.Escape:
StringBuilder pattern = new StringBuilder();
foreach (string s in arrayOfStringsToRemove)
{
pattern.Append("(");
pattern.Append(Regex.Escape(s));
pattern.Append(")|");
}
Regex.Replace(inputString, pattern.ToString(0, pattern.Length - 1), // remove trailing |
replacement);
Look at Regex.Escape
You need to escape special characters with a backslash
\
Sometimes you may need to use two backslashes
\\
You need to escape characters with spacial meaning of course.
var str_to_replace = "removing \[this\]\(link\)";

Categories