I have the following strings:
This is my testasdasd [Test(XYZ="P")] abc sdfsdf
This is my testasdasd [Test(ABC="P")] sdfsdf
This is my testdfsdfsdf [Test(DEF="P")] sdfsdfs
This is my testsdfsdfsdf [Test(GHI="P")] asdfasdasd
I want the add ", Hello" text after the ")" inn the above strings. My output should look like this:
This is my testasdasd [Test(XYZ="P"), Hello] abc sdfsdf
This is my testasdasd [Test(ABC="P"), Hello] sdfsdf
This is my testdfsdfsdf [Test(DEF="P"), Hello] sdfsdfs
This is my testsdfsdfsdf [Test(GHI="P"), Hello] asdfasdasd
Can you help me making regex for that?
EDIT: I can't do the above just by doing find and replace "]" I have other brackets too in my strings. I need to find [Test(..)] and the output should be [Test(...) , Hello]
You can try with the lookup expression:
#"(\[\s*Test\s*\(.*?\)\s*)\]"
and this replace expression:
"$1,Hello]"
replace regex (?<=\[Test\(\w+?="P"\))\] with string , Hello]
Try this if you do not want to use a regex:
String newString = yourString.replace( "]", ", Hello]" );
This will only work correctly, if there is only one "]" in your string..
If the content is going to be static. Trying string.replace will do. My view :)
function insert(string, word) {
return string.replace(/\[Test\(.+\)\]/g, function(a, b) {
return a.replace("]", ", " + word + "]");
});
}
insert('This is my testasdasd [Test(XYZ="P")] abc sdfsdf', "Hello");
Try this regex:
(\[Test\([^\)]*\))\]
replace with
$1, Hello]
your code can be like this:
var result = Regex.Replace(inputString, #"(\[Test\([^\)]*\))\]", "$1, Hello]");
or this simle replace:
var result = inputString.replace( "]", ", Hello]" );
Related
I have this string:
02-37-30-30-30-32-42-34-30-38-45-39-35-03
I want to remove the delimiter "-" so that the final output will be:
0237303030324234303845393503
How can I do that?
Try
myString.Replace ("-", "");
Tried this?
stringName.Replace("-", "");
This would replace all the -s with "" and will remove them.
If you want to use Regex, (although I'm not sure why that's beneficial) you can do it like this:
string result = Regex.Replace(
"02-37-30-30-30-32-42-34-30-38-45-39-35-03", "-", "");
I have a complex situation where I need to parse a very long string. I have to look for a pattern in the string and replace that pattern with another. I know I can simply use find/replace method but my case is bit different.
I have a string that contains the following pattern
#EPB_IMG#index-1_1.jpg#EPB_IMG
#EPB_IMG#index-1_2.jpg#EPB_IMG
#EPB_IMG#index-1_3.jpg#EPB_IMG
#EPB_IMG#index-1_4.jpg#EPB_IMG
and I want it to format as
#EPB_IMG#index-1_1.jpg|index-1_2.jpg|index-1_3.jpg|index-1_4.jpg#EPB_IMG
I don't know much about Regex and seeking for help.
Regex is overkill:
var parts = s.Split(new string[] { "#EPB_IMG", "#", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
var result = string.Join("|", parts);
Console.WriteLine("#EPB_IMG#" + result + "#EPB_IMG"); // prints your result.
Maybe something like this:
Expression: \w+#(?<Image>(.+)\.jpg)#\w+
Replacement: ${Image}
Result: string.Format("#EPB_IMG#{0}#EPB_IMG", string.Join("|", listOfMatches))
NOTE:
Regex tested with: http://regexhero.net/tester/
Result is untested, but should work!
var result = "#EPB_IMG" + Regex.Matches(inputString, #"#EPB_IMG(.+)#EPB_IMG")
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.Aggregate((f, f2) => f + "|" + f2) + "#EPB_IMG";
I have a string, which represents part of xml.
string text ="word foo<tag foo='a' />another word "
and I need to replace particular words in this string. So I used this code:
Regex regex = new Regex("\\b" + co + "\\b", RegexOptions.IgnoreCase);
return regex.Replace(text, new MatchEvaluator(subZvyrazniStr));
static string subZvyrazniStr(Match m)
{
return "<FtxFraze>" + m.ToString() + "</FtxFraze>";
}
But the problem of my code is, that it also replaces string inside tags, which i don't want to. So what should I add, to replace words only outside tags?
Ex.: when I set variable co to "foo" I want to return "word <FtxFraze>foo</FtxFraze><tag foo='a' />another word"
Thanks
A simple trick like this may suffice in some cases if you are not that picky:
\bfoo\b(?![^<>]*>)
This is what you want
(?<!\<[\w\s]*?)\bfoo\b(?![\w\s]*?>)
works here
I had answered a related question here
Try this regex:
Regex r = new Regex(#"\b" + rep + #".*?(?=\<)\b", RegexOptions.IgnoreCase);
It is very basic question but i am not sure why it is not working. I have code where 'And' can be written in any of the ways 'And', 'and', etc. and i want to replace it with ','
I tried this:
and.Replace("and".ToUpper(),",");
but this is not working, any other way to do this or make it work?
You should check out the Regex class
http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx
using System.Text.RegularExpressions;
Regex re = new Regex("\band\b", RegexOptions.IgnoreCase);
string and = "This is my input string with and string in between.";
re.Replace(and, ",");
words = words.Replace("AND", ",")
.Replace("and", ",");
Or use RegEx.
The Replace method returns a string where the replacement is visible. It does not modify the original string. You should try something along the lines of
and = and.Replace("and",",");
You can do this for all variations of "and" you may encounter, or as other answers have suggested, you could use a regex.
I guess you should take care if some words contain and, say "this is sand and sea". The word "sand" must not be influenced by the replacement.
string and = "this is sand and sea";
//here you should probably add those delimiters that may occur near your "and"
//this substitution is not universal and will omit smth like this " and, "
string[] delimiters = new string[] { " " };
//it result in: "this is sand , sea"
and = string.Join(" ",
and.Split(delimiters,
StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Length == 3 && s.ToUpper().Equals("AND")
? ","
: s));
I would also add smth like this:
and = and.Replace(" , ", ", ");
So, the output:
this is sand, sea
try this way to use the static Regex.Replace() method:
and = System.Text.RegularExpressions.Regex.Replace(and,"(?i)and",",");
The "(?i)" causes the following text search to be case-insensitive.
http://msdn.microsoft.com/en-us/library/yd1hzczs.aspx
http://msdn.microsoft.com/en-us/library/xwewhkd1(v=vs.100).aspx
I have a lot of strings that look like this:
current affairs
and i want to make the string be :
current affairs
i try to use Trim() but it won't do the job
Regex can do the job
string_text = Regex.Replace(string_text, #"\s+", " ");
You can use regular expressions for this, see Regex.Replace:
var normalizedString = Regex.Replace(myString, " +", " ");
If you want all types of whitespace, use #"\s+" instead of " +" which just deals with spaces.
var normalizedString = Regex.Replace(myString, #"\s+", " ");
Use a regular expression.
yourString= Regex.Replace(yourString, #"\s+", " ");
You can use a regex:
public string RemoveMultipleSpaces(string s)
{
return Regex.Replace(value, #"\s+", " ");
}
After:
string s = "current affairs ";
s = RemoveMultipleSpaces(s);
Using Regex here is the way,
System.Text.RegularExpressions.Regex.Replace(input, #”\s+”, ” “);
This will removes all whitespace characters including tabs, newlines etc.
First you need to split the whole string and then apply trim to each item.
string [] words = text.Split(' ');
text="";
forearch(string s in words){
text+=s.Trim();
}
//text should be ok at this time