RegExes work. Try this. Whether it is any better, I don't know
class RegexTest
{publicstaticvoid Main()
{string[] tests = {
@"XYZ Company [- Phone: [({404}) ]{321-4321} [Ext: {6789}]]",
@"XYZ Company [- Phone: [({404}) ]{321-4321} [Ext: {}]]",
@"XYZ Company [- Phone: [({}) ]{321-4321} [Ext: {}]]",
@"XYZ Company [- Phone: [({}) ]{6} [Ext: {}]]"
};foreach (string s in tests)
{string newValue = StoryManipulation(s);
Console.WriteLine("{0}\n{1}\n", s, newValue);
}
Console.ReadLine();
}
privatestaticstring StoryManipulation(string theStory)
{string item = @"(?<Item>\[[^[{]*[{]VariablePattern[}][^]]*\])";string variablePattern = @"(?<VariableContents>[^}]*)";string pattern = item.Replace("VariablePattern", variablePattern);
MatchCollection matches;while ((matches = Regex.Matches(theStory, pattern, RegexOptions.Singleline)).Count > 0)
{
Match match = matches[matches.Count -1];string s = match.Groups["Item"].ToString();if (match.Groups["VariableContents"].Length == 0)
{
theStory = theStory.Remove(match.Groups["Item"].Index, match.Groups["Item"].Length);
}else
{
theStory = theStory.Remove(match.Groups["Item"].Index + match.Groups["Item"].Length - 1, 1) // ]
.Remove(match.Groups["VariableContents"].Index + match.Groups["VariableContents"].Length, 1) // }
.Remove(match.Groups["VariableContents"].Index - 1, 1) // {
.Remove(match.Groups["Item"].Index , 1) // [
;
}
}
return theStory;
}
}
Hi Everyone,
This was presented to me by one of our graphic guys. I thought I'd post it to you guru's to play with too...
I suspect this would be better written using Regular Expressions but am not that proficient with them yet. Thoughts?
Thanks much, Craig
Given the string "story" below, write a script to do the following.
1. Variable text is enclosed by { }.
2. If the variable text is blank, remove any other text enclosed in [ ].
3. Text to be removed can be nested deep with [ ].
"XYZ Company [- Phone: [({404}) ]{321-4321} [Ext: {6789}]]"
Examples.
1. All variable text filled in.
XYZ Company - Phone: (404) 321-4321 Ext: 6789
2. No Extension entered, remove "Ext:".
XYZ Company - Phone: (404) 321-4321
3. No Extension and no area code entered, remove "Ext:" and "( ) ".
XYZ Company - Phone: 321-4321
4. No extension, no phone number, and no area code entered, remove "Ext:" and "( ) " and "- Phone: ".
XYZ Company
privatestring StoryManipulation(string theStory)
{while (theStory.IndexOf("{") > 0)
{string lcCurlyText = StringUtils.ExtractString(theStory, "{", "}"); if (String.IsNullOrWhiteSpace(lcCurlyText))
{for (int lnCounter = theStory.IndexOf("{"); lnCounter >= 0; lnCounter--)
{if (theStory.Substring(lnCounter - 1, 1) == "[")
{string lcSquareText = StringUtils.ExtractString(theStory.Substring(lnCounter - 1), "[", "]");
theStory = StringUtils.ReplaceString(theStory, ("[" + lcSquareText + "]"), "", false);break;
}
}
}else
{
theStory = StringUtils.ReplaceString(theStory, ("{" + lcCurlyText + "}"), lcCurlyText, false);
}
}
theStory = StringUtils.ReplaceStringInstance(theStory, "[", "", -1, false);
theStory = StringUtils.ReplaceStringInstance(theStory, "]", "", -1, false);return theStory.Trim();
}