Rexex replace

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I am curious why my Regex would not work when like this

tableName = Regex.Replace(tableName, "'$", "")

But worked fine like this:

tableName = tableName.Replace("$", "");

Thanks,

Tom
 
tshad said:
I am curious why my Regex would not work when like this

tableName = Regex.Replace(tableName, "'$", "")

But worked fine like this:

tableName = tableName.Replace("$", "");

Because the two are not the same? I don't know what you intend the "'$"
to do, but as far as I know it won't be anything useful in Regex. I
don't think it's the same as "find all instances of the '$' character".

I think probably you want "\$" as your expression, but of course there
remains the question of why use Regex when the String.Replace() method
already does exactly what you want. The String method will be much more
efficient too.

Pete
 
I am curious why my Regex would not work when like this

tableName = Regex.Replace(tableName, "'$", "")

But worked fine like this:

tableName = tableName.Replace("$", "");

Well, let's see. Your regex pattern is <apostrophe><dollar sign>. (Really,
that's what's up there.)

So that tells the regex engine to find any apostrophes at the end of a line
and replace them with an empty string.
 
Jeff Johnson said:
Well, let's see. Your regex pattern is <apostrophe><dollar sign>. (Really,
that's what's up there.)

You're right.

I couldn't see the apostrophe.

Thanks,

Tom
 
Peter Duniho said:
Because the two are not the same? I don't know what you intend the "'$"
to do, but as far as I know it won't be anything useful in Regex. I don't
think it's the same as "find all instances of the '$' character".
As Jeff mentioned, there is an apostrophe there that I didn't see and it
should delete all "$", unless you need the "\" in front of it. Not sure
about that.
I think probably you want "\$" as your expression, but of course there
remains the question of why use Regex when the String.Replace() method
already does exactly what you want. The String method will be much more
efficient too.

It was there because it was actually part of a couple of other Regex
statements. I pulled it out to test it.

I did end up using the String.Replace but probably the Regex would also work
if it didn't have the apostrophe.

Thanks,

Tom
 
Back
Top