Regex replace help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Julius Fuchs

    Regex replace help

    Hi,

    I have a string like "bla<cut>blubb< cut>bla<cut>blu bb" and want to replace
    the substring from the first occurrence of <cut> to the second with "TEST"
    in order to get "blaTESTbla<cut >blubb".
    So far I only managed to get blaTESTblubb:

    Regex iRegex = new Regex("<cut>.*< cut>");
    return iRegex.Replace( "bla<cut>blubb< cut>bla<cut>blu bb", "TEST");


  • Martin Honnen

    #2
    Re: Regex replace help



    Julius Fuchs wrote:

    [color=blue]
    > Regex iRegex = new Regex("<cut>.*< cut>");[/color]

    You want non-greedy matching I think
    Regex iRegex = new Regex("<cut>.*? <cut>");


    --

    Martin Honnen --- MVP XML

    Comment

    • Julius Fuchs

      #3
      Re: Regex replace help

      Martin Honnen wrote:[color=blue]
      > You want non-greedy matching I think
      > Regex iRegex = new Regex("<cut>.*? <cut>");[/color]

      Thanks for your answer but that's not exactly what I wanted. Imagine the
      following string: "a<cut>b<cut>c< cut>d<cut>e", with "<cut>.*?<c ut>" as
      pattern I would get "aTESTcTEST e" instead of "aTESTc<cut>d<c ut>e" what I
      want.


      Comment

      • AlanT

        #4
        Re: Regex replace help


        The lazy (non-greedy) match indicated by Martin works but the lazy
        matches are generally much slower than that standard (greedy) matches.

        If performance is an issue you might look at

        Regex iRegex = new Regex("<cut>\b\ w*\b<cut>");

        The \b (word boundary) prevents matching across multiple <cut> tags
        NOTE: This will not find empty pairs (e.g. in bla<cut><cut>dd sd<cut>,
        it will not find the <cut><cut>)

        or

        Regex iRegex = new Regex("<cut>((? !<cut>).)*<cut> ")

        where the negative lookahead prevents us passing the middle <cut>.
        NOTE: This will find the empty pair

        hth,
        Alan

        Comment

        • AlanT

          #5
          Re: Regex replace help


          Use

          return iRegex.Replace( "a<cut>b<cut>c< cut>d<cut>e", "TEST", 1);

          This will return the string with only the first match replaced.

          aTESTc<cut>d<cu t>e



          hth,
          Alan.

          Comment

          Working...