Regex Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harrydeo
    New Member
    • Feb 2007
    • 3

    Regex Help

    Hi To all

    This question is with regard to Regular Expressions. I want to meet two objectives.

    1. In Html pages, I want two remove starting numbers from title tag.

    For example

    From
    <title> 2.3.4_File Is Good </title>
    Replaced By
    <title> File Is Good </title>

    2. Also I want to remove trailing spaces from DOC.

    From
    2.3.4 Kunal Deo
    To
    Kunal Deo

    Thank you very much any help will be highly appreciated.
  • battlesc
    New Member
    • Feb 2007
    • 3

    #2
    Try this to remove the non-alphabetic stuff at the beginning of the string.

    String strInput = "2.3.4_File Is Good ";
    String strOutput = String.Empty;

    Regex regEx = new Regex(@"^[^a-zA-Z]*|\s*$", RegexOptions.Ig noreCase | RegexOptions.Mu ltiline);
    strOutput = regEx.Replace(s trInput, "");
    Console.WriteLi ne("START------" + strOutput + "-------END");

    Comment

    • harrydeo
      New Member
      • Feb 2007
      • 3

      #3
      Thanks battlesc for your help.

      Apart from that Is there any way to achive a global find and replace seach in one directory (or recursively) and Replaces all <Title> tags text only.

      Comment

      • harrydeo
        New Member
        • Feb 2007
        • 3

        #4
        Program

        String strInput = "<title>2.4.4_C hapter1</title>";
        String strOutput = String.Empty;

        Regex regEx = new Regex(@"^[^a-zA-Z]*|\s*$", RegexOptions.Ig noreCase | RegexOptions.Mu ltiline);
        strOutput = regEx.Replace(s trInput, "");
        Console.WriteLi ne("START------\n" + strOutput + "\n-------END");

        When I run this I get following output :--------

        START------
        title>2.4.4_Cha pter1</title>
        -------END
        Press any key to continue . . .

        Any Solution

        Comment

        • battlesc
          New Member
          • Feb 2007
          • 3

          #5
          I assume that you will have the title part in a separate string, so that the regex I provided will work. This could be done multiple ways, using either regex of your own, or use the XML xpath stuff to get them.

          Here is an example of using your own regex:
          using System;
          using System.Collecti ons.Generic;
          using System.Text;
          using System.Text.Reg ularExpressions ;

          namespace RegexPrac1
          {
          class Program
          {
          public static Regex regExTitleConte nt = new Regex(@"^[^a-zA-Z]*|\s*$", RegexOptions.Ig noreCase | RegexOptions.Mu ltiline);
          public static Regex regExFullTitle = new Regex(@"(?<star ttag><title>)(? <titlecontent >[^<]*)(?<endtag></title>)", RegexOptions.Ig noreCase | RegexOptions.Mu ltiline);

          static void Main(string[] args)
          {
          String strInput = "what ever <title> 2.3.4_File Is Good </title> or something more <title> 12 Yes </title> blah etc";

          MatchEvaluator evaluator = new MatchEvaluator( ProcessTitle);
          String strOutput = regExFullTitle. Replace(strInpu t, evaluator);

          Console.WriteLi ne("START------" + strOutput + "-------END");
          }

          static string ProcessTitle(Ma tch match)
          {
          string fullTitle = match.Groups["titleconte nt"].Value;
          return match.Groups["starttag"].Value + regExTitleConte nt.Replace(full Title, "") + match.Groups["endtag"].Value;
          }

          }
          }

          Comment

          Working...