Regex Bug???

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

    Regex Bug???

    I am noticing some really strange behavior with Regex replacements and
    am wondering if anyone can help me make any sense of it.

    Let's say we have a string "Price=#InsertP rice#" and we want to
    replace "#InsertPri ce# with one or more "$" symbols to denote the
    price level.

    Now, here's what I'm seeing when I try the following replacement
    strings:

    Replacement String Output
    ---------------------------------------------------------------------------------------
    "$" - "Price=
    $"
    "$$" - "Price=
    $"
    "$$$" - "Price=$
    $"

    What appears to be happening is that it's truncating the first "$"
    when the output string is being built.

    My first thought is that this might be because the "$" symbol is a
    special character used for substitutions and I needed to escape it.
    But when I try to escape it, it doesn't work either. I get:

    @"\$\$" - "Price=\$\
    $"

    At this point I have no idea what is going on here and all I can think
    of is that this must be a bug. Any ideas?
  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: Regex Bug???

    Alphamacaroon wrote:
    I am noticing some really strange behavior with Regex replacements and
    am wondering if anyone can help me make any sense of it.
    >
    Let's say we have a string "Price=#InsertP rice#" and we want to
    replace "#InsertPri ce# with one or more "$" symbols to denote the
    price level.
    >
    Now, here's what I'm seeing when I try the following replacement
    strings:
    >
    Replacement String Output
    ---------------------------------------------------------------------------------------
    "$" - "Price=
    $"
    May we see a code snippet illustrating the problem ?

    Arne

    Comment

    • Alain Boss

      #3
      Re: Regex Bug???

      Alphamacaroon schrieb:
      I am noticing some really strange behavior with Regex replacements and
      am wondering if anyone can help me make any sense of it.
      >
      Let's say we have a string "Price=#InsertP rice#" and we want to
      replace "#InsertPri ce# with one or more "$" symbols to denote the
      price level.
      >
      Now, here's what I'm seeing when I try the following replacement
      strings:
      >
      Replacement String Output
      ---------------------------------------------------------------------------------------
      "$" - "Price=
      $"
      "$$" - "Price=
      $"
      "$$$" - "Price=$
      $"
      >
      What appears to be happening is that it's truncating the first "$"
      when the output string is being built.
      >
      My first thought is that this might be because the "$" symbol is a
      special character used for substitutions and I needed to escape it.
      But when I try to escape it, it doesn't work either. I get:
      >
      @"\$\$" - "Price=\$\
      $"
      >
      At this point I have no idea what is going on here and all I can think
      of is that this must be a bug. Any ideas?
      your problem might be that '/' is also a special c# character. please
      post some code, as arne suggested, or try your regex using @"" for your
      regex-definition.

      alain

      Comment

      • Jesse Houwing

        #4
        Re: Regex Bug???

        Hello Alphamacaroon,
        I am noticing some really strange behavior with Regex replacements and
        am wondering if anyone can help me make any sense of it.
        >
        Let's say we have a string "Price=#InsertP rice#" and we want to
        replace "#InsertPri ce# with one or more "$" symbols to denote the
        price level.
        >
        Now, here's what I'm seeing when I try the following replacement
        strings:
        >
        Replacement String Output
        ----------------------------------------------------------------------
        -----------------
        "$" - "Price=
        $"
        "$$" - "Price=
        $"
        "$$$" - "Price=$
        $"
        What appears to be happening is that it's truncating the first "$"
        when the output string is being built.
        >
        My first thought is that this might be because the "$" symbol is a
        special character used for substitutions and I needed to escape it.
        But when I try to escape it, it doesn't work either. I get:
        >
        @"\$\$" ->
        "Price=\$\ $"
        >
        At this point I have no idea what is going on here and all I can think
        of is that this must be a bug. Any ideas?

        $ is indeed special in a replaceemnt string. And you can escape it (as you've
        already done in your second example) by doubling it up, like this: $$.

        --
        Jesse Houwing
        jesse.houwing at sogeti.nl


        Comment

        • Alphamacaroon

          #5
          Re: Regex Bug???

          On Apr 29, 11:45 am, Jesse Houwing <jesse.houw...@ newsgroup.nospa m>
          wrote:
          Hello Alphamacaroon,
          >
          >
          >
          I am noticing some really strange behavior with Regex replacements and
          am wondering if anyone can help me make any sense of it.
          >
          Let's say we have a string "Price=#InsertP rice#" and we want to
          replace "#InsertPri ce# with one or more "$" symbols to denote the
          price level.
          >
          Now, here's what I'm seeing when I try the following replacement
          strings:
          >
          Replacement String Output
          ----------------------------------------------------------------------
          -----------------
          "$" - "Price=
          $"
          "$$" - "Price=
          $"
          "$$$" - "Price=$
          $"
          What appears to be happening is that it's truncating the first "$"
          when the output string is being built.
          >
          My first thought is that this might be because the "$" symbol is a
          special character used for substitutions and I needed to escape it.
          But when I try to escape it, it doesn't work either. I get:
          >
          @"\$\$" ->
          "Price=\$\ $"
          >
          At this point I have no idea what is going on here and all I can think
          of is that this must be a bug. Any ideas?
          >
          $ is indeed special in a replaceemnt string. And you can escape it (as you've
          already done in your second example) by doubling it up, like this: $$.
          >
          --
          Jesse Houwing
          jesse.houwing at sogeti.nl
          Ahhh. Makes sense. So unlike RegEx Pattern strings where the escape
          char is '\', the escape char for the replacement string is '$' itself.
          I'll give it a try, but I suspect that will fix it. Thanks!

          BTW: Is this documented anywhere?

          Comment

          Working...