Results 1 to 4 of 4

Thread: Regex help

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Regex help

    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:


    Code:
    if (Regex.IsMatch("blahblah", "[me]", RegexOptions.IgnoreCase))
    {
       //whatever
    }

    The other thing is, how can I make this:

    [fc=red][/fc]

    into:

    <someTag=red></someTag>

    in Regex?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: Regex help

    Oooh boy


    Give this a try:

    Code:
    Regex r = new Regex(
       @"\[(?<tag>[A-Za-z]+)=(?<value>[^\]]+)\](?<text>[^\[]*)\[(?<endtag>/[A-Za-z]+)\]"
    );
    
    string s = "[fc=red]hello world[/fc]";
    
    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);
    }
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Regex help

    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?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Regex help

    found it, got it working, almost but still need help.

    basically this seems to work:

    Code:
    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:

    Code:
    [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!

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width