Regex Bug???

A

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=#InsertPrice#" and we want to
replace "#InsertPrice# 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?
 
A

Arne Vajhøj

Alphamacaroon said:
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=#InsertPrice#" and we want to
replace "#InsertPrice# 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

May we see a code snippet illustrating the problem ?

Arne
 
A

Alain Boss

Alphamacaroon said:
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=#InsertPrice#" and we want to
replace "#InsertPrice# 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
 
J

Jesse Houwing

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=#InsertPrice#" and we want to
replace "#InsertPrice# 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: $$.
 
A

Alphamacaroon

Hello Alphamacaroon,










$ 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: $$.

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top