Click to See Complete Forum and Search --> : Regex help
Techno
Apr 23rd, 2006, 06:29 PM
I thought about using string.Replace() which is fine but then I wondered if I should be using Regex instead?
I pretty much want to be able to replace strings giving it the string to look for, if there is a match then to replace it.
Currently this always evaluates to true, but it shouldnt be as you can see:
if (Regex.IsMatch("blahblah", "[me]", RegexOptions.IgnoreCase))
{
//whatever
}
The other thing is, how can I make this:
into:
<someTag=red></someTag>
in Regex?
sunburnt
Apr 23rd, 2006, 06:53 PM
Oooh boy :D
Give this a try:
Regex r = new Regex(
@"\[(?<tag>[A-Za-z]+)=(?<value>[^\]]+)\](?<text>[^\[]*)\[(?<endtag>/[A-Za-z]+)\]"
);
string s = "hello world";
MatchCollection mm = r.Matches(s);
foreach (Match m in mm)
{
Console.WriteLine("<{0}={1}>{2}<{3}>",
m.Groups["tag"].Value,
m.Groups["value"].Value,
m.Groups["text"].Value,
m.Groups["endtag"].Value);
}
Techno
Apr 24th, 2006, 06:03 AM
lol wow ill give it a shot, but found this seems to work, however your code - I will check right now. This is what I had as a Regex:
\\[fc=.*?]
but afterwards, how can I replace the string with the edited tag?
Techno
Apr 24th, 2006, 06:39 PM
found it, got it working, almost but still need help.
basically this seems to work:
Match theOpeningFontTagMatch = Regex.Match(theTextToAnalyze, "\\[fc=.*?]", RegexOptions.IgnoreCase);
if (theOpeningFontTagMatch.Success)
{
string originalMatchValue = theOpeningFontTagMatch.Value;
string removedCode = originalMatchValue.Replace("[fc=", String.Empty);
removedCode = removedCode.Replace("]", String.Empty);
theTextToAnalyze = Regex.Replace(theTextToAnalyze, "\\[fc=.*?]", "<font color=" + removedCode + ">");
}
it works, and does what I want it to but when there are more than 1 [fc=] tag, and after the = there will be a different value, it seems to ignore it and replaces all of them with the first occurence. so if we had:
[fc=red]
[fc=green]
[fc=white]
it would replace them all with red, rather than red, green, white and so on
what am I doing wrong? please help, im almost there! :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.