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";
Related
Question: How do I write an expression to split a string on ',' but not '/,'? Later I'll want to replace '/,' with ', '.
Details...
Delimiter: ','
Skip Char: '/'
Example input: "Mister,Bill,is,made,of/,clay"
I want to split this input into an array: {"Mister", "Bill", "is", "made", "of, clay"}
I know how to do this with a char prev, cur; and some indexers, but that seems beta.
Java Regex has a split functionality, but I don't know how to replicate this behavior in C#.
Note: This isn't a duplicate question, this is the same question but for a different language.
I believe you're looking for a negative lookbehind:
var regex = new Regex("(?<!/),");
var result = regex.Split(str);
this will split str on all commas that are not preceded by a slash. If you want to keep the '/,' in the string then this will work for you.
Since you said that you wanted to split the string and later replace the '/,' with ', ', you'll want to do the above first then you can iterate over the result and replace the strings like so:
var replacedResult = result.Select(s => s.Replace("/,", ", ");
string s = "Mister,Bill,is,made,of/,clay";
var arr = s.Replace("/,"," ").Split(',');
result : {"Mister", "Bill", "is", "made", "of clay"}
Using Regex:
var result = Regex.Split("Mister,Bill,is,made,of/,clay", "(?<=[^/]),");
Just use a Replace to remove the commas from your string :
s.Replace("/,", "//").Split(',').Select(x => x.Replace("//", ","));
You can use this in c#
string regex = #"(?:[^\/]),";
var match = Regex.Split("Mister,Bill,is,made,of/,clay", regex, RegexOptions.IgnoreCase);
After that you can replace /, and continue your operation as you like
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
string CCstring = "CC01=50 CC02=300 CC03=500 CC04=40";
I want to store the individual values in seperate strings like:
for(int i = 0; i<=4; i++)
{
string suffix = i.ToString().PadLeft(2, '0');
string CCindividual = CCindividual + i;
CCindividual = //THIS IS WHERE I WOULD LIKE TO GET MY INDIVIDUAL VALUES i.e 50,300,500,40;
Console.WriteLn("CC" + i + " =" + CCIndividual);//Testing
}
Which string manipulation should I use Regex or Substring. How would the code snippet look like?
One line:
string[] CCindividual = Regex.Split(CCstring, "CC[0-9]+=").Where(x => x != "").
Select(x => x.Trim()).ToArray<String>();
Not sure this is the more efficient way though.
Can't you use split to first split on spaces and next on '='? It's easier than regex or substring imho.
Neither. You can use string.Split to get an array:
string CCstring = "CC01=50 CC02=300 CC03=500 CC04=40";
string[] strings = CCstring.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
After that, you are able to do the same for the = using string.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);.
Unless you need this code to run very very efficiently. You should be worried about what is readable for you and your team (sometimes thats substring, split etc and sometimes thats regex). Only you can really decide.
just use String.Split
string CCstring = "CC01=50 CC02=300 CC03=500 CC04=40";
var result = CCstring.Split(' ')
.Select(s => s.Split('='))
.ToDictionary(kv => kv[0], kv => Convert.ToInt64(kv[1]));
Looking at your CCstring it will be definitely faster to walk through the string characters just once. Sure it doesn't worth that until you have tons of such strings.
So, yep, it's easier to use just string.Split once for spaces, and once for each fragmet to split by '='.
Given the c# code:
string foo = #"
abcde
fghijk";
I am trying to remove all formatting, including whitespaces between the lines.
So far the code
foo = foo.Replace("\n","").Replace("\r", "");
works but the whitespace between lines 2 and 3 and still kept.
I assume a regular expression is the only solution?
Thanks.
I'm assuming you want to keep multiple lines, if not, i'd choose CAbbott's answer.
var fooNoWhiteSpace = string.Join(
Environment.NewLine,
foo.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
.Select(fooline => fooline.Trim())
);
What this does it split the string into lines (foo.Split),
trim whitespace from the start and end of each line (.Select(fooline => fooline.Trim())),
then combine them back together with a new line inbetween (string.Join).
You could use a regular expression:
foo = Regex.Replace(foo, #"\s+", "");
How about this?
string input = #"
abcde
fghijk";
string output = "";
string[] parts = input.Split('\n');
foreach (var part in parts)
{
// If you want everything on one line... else just + "\n" to it
output += part.Trim();
}
This should remove everthing.
If the whitespace is all spaces, you could use
foo.Replace(" ", "");
For any other whitespace that may be in there, do the same. Example:
foo.Replace("\t", "");
Just add a Replace(" ", "") your dealing with a string literal which mean all the white space is part of the string.
Try something like this:
string test = #"
abcde
fghijk";
EDIT: Addded code to only filter out white spaces.
string newString = new string(test.Where(c => Char.IsWhiteSpace(c) == false).ToArray());
Produces the following: abcdefghijk
I've written something similar to George Duckett but put my logic into a string extension method so it easier for other to read/consume:
public static class Extensions
{
public static string RemoveTabbing(this string fmt)
{
return string.Join(
System.Environment.NewLine,
fmt.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
.Select(fooline => fooline.Trim()));
}
}
you can the call it like this:
string foo = #"
abcde
fghijk".RemoveTabbing();
I hope that helps someone
Assume I have the string:
10,11,12,13,14,ABC,DEF,GHI,66
I am looking to run a regex against it to only return 0-9 and the "," characters, essentially stripping anything else out.
I have looked at Regex.Replace, but something isn't quite right with it. My code below:
Regex reg = new Regex(#"[0-9,]+");
string input = reg.Replace(input, delegate(Match m)
{
return String.Empty;
});
How can I make this work?
Do you just want a ^ in that?
input = Regex.Replace(input, #"[^0-9,]+", "");
Would a match collection give you more control?
Using \d+[^,] you can get a collection of digits?
You could then loop through your collection and recreate your desired string.
using linq you could do the following:
var input = "10,11,12,13,14,ABC,DEF,GHI,66";
Regex re = new Regex(#"\d+[^,]");
input = (from Match m in re.Matches(input) select m.Value).Aggregate("", (acc, item) => acc + "," + item).TrimStart(',');
How about this:
var testString = "10,11,12,13,14,ABC,DEF,GHI,66";
var split = testString.Split(',');
var result = String.Join(",", split.Where(element => element.All(c => Char.IsDigit(c))).ToArray());
I think that you may do it without regular expressions via analysis of what is required for your characters in set [0123456789,].