Regular Expression?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?VEo=?=

    Regular Expression?


    Hi,

    I want to know how Regular Expression can be used in this situation.
    I want to replace some string in specific condition..
    The condition is to replace string only if the string is NOT inside of tag.

    For example,
    <Test id='wow'>wow</Test>
    I want to replace the wow with great...So output will be
    <Test id='wow'>great</Test>

    As you know, if I just simply use 'wow' regular expression, it won't work...

    Anybody knows what regular expression am I supposed to use?


    Thanks,

  • Jon Skeet [C# MVP]

    #2
    Re: Regular Expression?

    TJ <TJ@discussions .microsoft.comw rote:
    I want to know how Regular Expression can be used in this situation.
    I want to replace some string in specific condition..
    The condition is to replace string only if the string is NOT inside of tag.
    >
    For example,
    <Test id='wow'>wow</Test>
    I want to replace the wow with great...So output will be
    <Test id='wow'>great</Test>
    >
    As you know, if I just simply use 'wow' regular expression, it won't work...
    >
    Anybody knows what regular expression am I supposed to use?
    Do you absolutely have to use a regular expression for this? Personally
    I find that trying to work with XML using basic string handling is a
    lot more error prone than using the XML APIs that are available. Is
    there anything to stop you from iterating through all of the available
    text nodes and just doing simple replacements within them?

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • =?Utf-8?B?VEo=?=

      #3
      Re: Regular Expression?

      Hi,

      Thanks for your reply.
      That's also one option I think.

      However, actually, the input string is not XML.
      I just gave the example for simplication.

      Actual input string is HTML. It could be whole HTML, or part of HTML.
      The thing I want is that I want to replace some string, if the string is NOT
      inside of tag.

      For example,

      <Img src='wow.jpg'>w ow</img>

      The output would be <Img src='wow.jpg'>g reat</img>

      If you have any other idea other than regular expression, please share it
      with us.

      Thanks,




      "Jon Skeet [C# MVP]" wrote:
      TJ <TJ@discussions .microsoft.comw rote:
      I want to know how Regular Expression can be used in this situation.
      I want to replace some string in specific condition..
      The condition is to replace string only if the string is NOT inside of tag.

      For example,
      <Test id='wow'>wow</Test>
      I want to replace the wow with great...So output will be
      <Test id='wow'>great</Test>

      As you know, if I just simply use 'wow' regular expression, it won't work...

      Anybody knows what regular expression am I supposed to use?
      >
      Do you absolutely have to use a regular expression for this? Personally
      I find that trying to work with XML using basic string handling is a
      lot more error prone than using the XML APIs that are available. Is
      there anything to stop you from iterating through all of the available
      text nodes and just doing simple replacements within them?
      >
      --
      Jon Skeet - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon.skeet
      C# in Depth: http://csharpindepth.com
      >

      Comment

      • Jeroen Mostert

        #4
        Re: Regular Expression?

        TJ wrote:
        Actual input string is HTML. It could be whole HTML, or part of HTML.
        The thing I want is that I want to replace some string, if the string is NOT
        inside of tag.
        >
        Regular expression examples for processing HTML are all over the web. Here's
        the third hit I got from Google for "html regex tag", for example:


        However, if you want to do it right and take into account things like
        unclosed tags ("<br>"), mismatched tags and CDATA sections, a regex will not
        help you (or else it'll be way too complicated to write); you'll need an
        actual HTML parser. Google suggests there are multiple for .NET, some of
        which might be useful.

        --
        J.

        Comment

        Working...