I used Regex.Replace(jobdesc, #"[^\/\'-]+", " "); but its not working please help me I want to replace only (' and -) with blank space how to arrange this regex code.
You can use a simple replace like:
"your sentence".Replace("''"," ").Replace("-"," ");
Using Reg Exp you can do as below
string pattern = #"\-\'";
string input = "mynam-'is";
string replacement = " ";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);
Related
I'm trying to replace a string,enclosed in curly brackets.
If I use the Replace method provided by the Regex class and I don't specify the curly brackets, the string is found and replaced correctly, but if I do specify the curly brackets like this: {{FullName}}, the text is left untouched.
var pattern = "{{" + keyValue.Key + "}}";
docText = new Regex(pattern, RegexOptions.IgnoreCase).Replace(docText, keyValue.Value);
Take this string as a example
Dear {{FullName}}
I want to replace it with John, so that the text ends up like this:
Dear John.
How can I express the regex, so that the string is found and replace correctly?
You don't need a regular expression if the key is just a string. Just replace "{{FullName}}" with "John". example:
string template = "Dear {{FullName}}";
string result = template.Replace("{{" + keyValue.Key + "}}", keyValue.Value);
Edit: addressing concerns that this doesn't work...
The following is a complete example. You can run it at https://dotnetfiddle.net/wnIkvf
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var keyValue = new KeyValuePair<string,string>("FullName", "John");
string docText = "Dear {{FullName}}";
string result = docText.Replace("{{" + keyValue.Key + "}}", keyValue.Value);
Console.WriteLine(result);
}
}
var keyValue = new KeyValuePair<string,string>("FullName", "John");
var pattern = "{{" + keyValue.Key + "}}";
Console.WriteLine(new Regex(Regex.Escape(pattern), RegexOptions.IgnoreCase).Replace("Dear {{FullName}}", keyValue.Value));
Output:
Dear John
Looking for "Dear {{FullName}}" to "Dear John"?
not a regex solution... but this is how I prefer to do it sometimes.
string s = "Dear {{FullName}}";
// use regex to replace FullName like you mentioned before, then...
s.Replace("{",string.empty);
s.Replace("}",string.empty);
If you actually want to use a regular expression, then escape your literal text to turn it into a regular expression pattern using Regex.Escape.
var keyValue = new KeyValuePair<string,string>("FullName", "John");
string docText = "Dear {{FullName}}";
var pattern = "{{" + keyValue.Key + "}}";
docText = new Regex(Regex.Escape(pattern), RegexOptions.IgnoreCase).Replace(docText, keyValue.Value);
docText will be Dear John
What I believe is that you really want to do is replace multiple things in a document.
To do so use the regex pattern I provide, but also use the regex replace match evaluator delegate. What that does, is that every match can be actively evaluated for each item and a proper item will be replaced as per C# logic.
Here is an example with two possible keywords setup.
string text = "Dear {{FullName}}, I {{UserName}} am writing to say what a great answer!";
string pattern = #"\{\{(?<Keyword>[^}]+)\}\}";
var replacements
= new Dictionary<string, string>() { { "FullName", "OmegaMan" }, { "UserName", "eddy" } };
Regex.Replace(text, pattern, mt =>
{
return replacements.ContainsKey(mt.Groups["Keyword"].Value)
? replacements[mt.Groups["Keyword"].Value]
: "???";
}
);
Result
Dear OmegaMan, I eddy am writing to say what a great answer!
The preceding example uses
Match Evaluator Delegate
Named match capture groups (?<{Name here}> …)
Set Negation [^ ] which says match until the negated item is found, in this case a closing curly }.
I have the following string:
Hey this is a test
I'm trying to extract it using the following regex:
string buffer = "Hey this is a test";
Regex r = new Regex("Hey this is a (.*?)", RegexOptions.Multiline);
Match m = r.Match(buffer);
But for some reason I cannot extract it. Any suggestions? Thank you!
.*? tries to take minimum amount of chars. In your case zero.
() is a group. So the result be in m.Groups[1]
string buffer = "Hey this is a test";
Regex r = new Regex("Hey this is a (.*)", RegexOptions.Multiline);
Match m = r.Match(buffer);
Console.WriteLine(m.Groups[1]); // test
It is better to use more simple code. For example to take last word from string you can split the string by ' ' and take the last element:
string buffer = "Hey this is a test";
Console.WriteLine(buffer.Split(' ').Last());
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");
Is there a regex pattern that can remove .zip.ytu from the string below?
werfds_tyer.abc.zip.ytu_20111223170226_20111222.20111222
Here is an answer using regex as the OP asked.
To use regex, put the replacment text in a match ( ) and then replace that match with nothing string.Empty:
string text = #"werfds_tyer.abc.zip.ytu_20111223170226_20111222.20111222";
string pattern = #"(\.zip\.ytu)";
Console.WriteLine( Regex.Replace(text, pattern, string.Empty ));
// Outputs
// werfds_tyer.abc_20111223170226_20111222.20111222
Just use String.Replace()
String.Replace(".zip.ytu", "");
You don't need regex for exact matches.
txt = txt.Replace(".zip.ytu", "");
Why don't you simply do above?
Don't really know what is the ".zip.ytu", but if you don't need exact matches, you might use something like that:
string txt = "werfds_tyer.abc.zip.ytu_20111223170226_20111222.20111222";
Regex mRegex = new Regex(#"^([^.]*\.[^.]*)\.[^.]*\.[^_]*(_.*)$");
Match mMatch = mRegex.Match(txt);
string new_txt = mRegex.Replace(txt, mMatch.Groups[1].ToString() + mMatch.Groups[2].ToString());
use string.Replace:
txt = txt.Replace(".zip.ytu", "");
Here is the method I use for more complex repaces. Check out the link: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.replace(v=vs.110).aspx for a Regular Expression replace. I added the code below as well.
string input = "This is text with far too much " +
"whitespace.";
string pattern = "\\s+";
string replacement = " ";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);
Console.WriteLine("Original String: {0}", input);
Console.WriteLine("Replacement String: {0}", result);
I am trying to remove " from a string using Regex.
I am receiving a string into a method, I would like to take the string and split it up into the words that are in the string.
My Code is below, hopefully you can see what I am doing.
The problem I am having is trying to tell Regex that " is what I would like to remove. I have tried numerous ways: I have searched Google for a answer and have had to resort to here.
search_string looks like this: blah="blah" la="la" ta="ta" and in the end I want just the blah blah la la ta ta.
public blahblah blahblah(blah blah, string search_string)
{
Regex r = new Regex(#"/"+");
string s3 = r.Replace(search_string, #" ");
Regex r2 = new Regex(" ");
Regex r3 = new Regex("=");
string[] new_Split = { };
string[] split_String = r2.Split(s3);
foreach (string match in split_String)
{
new_Split = r3.Split(match);
}
//do blahblah stuff with new_Split[1] .. etc
// new_Split[0] should be blah and new_Split[1] should
// be blah with out "", not "blah"
return blah_Found;
Just use:
myString = myString.Replace( "\"", String.Empty );
[Update]
The String.Empty or "" is not a space char. You wrote this
blah="blah" la="la" ta="ta"
you want to convert to
blah blah la la ta ta
So you have white spaces anyway. If you want this:
blahblahlalatata
you need to remove them too:
myString = myString.Replace( "\"", String.Empty ).Replace( " ", String.Empty );
for '=' do it again, and so on...
You need to be more precise in your questions.
As a quick thought - and barking maybe up entirely the wrong tree, but wouldnt you want something like
Regex r = new Regex("(\".*\")");
eg, a reg expression of ".*"
This is one way to do it.
It will Search for anything in that form: SomeWord="somethingelse"
and replace it with SomeWord somethingelse
var regex = new Regex(#"(\w+)=\""(.+)\""");
var result = regex.Replace("bla=\"bla\"", "$1 $2");
I can't help you with Regex.
Anyway if you only need to remove = and " and split words you could try:
string[] arr = s
.Replace("="," ")
.Replace("\""," ")
.Split(new string[1] {" "}, StringSplitOptions.RemoveEmptyEntries);
I did it in 2 passes
string input = "blah=\"blah\" la=\"la\" ta=\"ta\"";
//replace " and = with a space
string output = Regex.Replace(input, "[\"=]", " ");
//condense the spaces
output = Regex.Replace(output, #"\s+", " ");
EDIT:
Treating " and = differently as per comment.
string input = "blah=\"blah\" la=\"la\" ta=\"ta\"";
//replace " and = with a space
string output = Regex.Replace(input, "\"", String.Empty);
output = Regex.Replace(output, "=", " ");
Clearly regex is a bit overkill here.