Regex mixing metacharacters

  • Thread starter Thread starter Phil396
  • Start date Start date
P

Phil396

I am trying to mix metacharacters and the literal versions
in an expression and no matter what I try it does not
work.

Here is the code

customer = Regex.Replace(customer,"[.]$","");

I want to remove all periods from the end of customer
names. However I really need to know how to
mix metacharacters and the literal versions ( mostly
periods ) in an expression. I have tried using \.
@ and other ways to escape out the period but with
zero success.
 
customer = Regex.Replace(customer,"\\.$","");
or
customer = Regex.Replace(customer,@"\.$","");

Should both work fine. Your version ("[.]$") is considered "bad style", but
should nevertheless work. Post a short sample if it doesn't.

Niki
 
Thanks for the information. I am doing regex
on web pages and spacing problems have been a nightmare.
Here is another case in point - there was an extra
space after the end of the period which made my
original expression incorrect.



-----Original Message-----
customer = Regex.Replace(customer,"\\.$","");
or
customer = Regex.Replace(customer,@"\.$","");

Should both work fine. Your version ("[.]$") is considered "bad style", but
should nevertheless work. Post a short sample if it doesn't.

Niki

Phil396 said:
I am trying to mix metacharacters and the literal versions
in an expression and no matter what I try it does not
work.

Here is the code

customer = Regex.Replace(customer,"[.]$","");

I want to remove all periods from the end of customer
names. However I really need to know how to
mix metacharacters and the literal versions ( mostly
periods ) in an expression. I have tried using \.
@ and other ways to escape out the period but with
zero success.


.
 
I expected something like that. You should maybe use some Regex tool like
"expresso" or "regulator" - those make it really easy to test regex's, and
they can create correct copy&paste C#-code.

And, thinking about it: maybe your regex should be tolerant enough to allow
spaces after customer names...

Niki

Phil396 said:
Thanks for the information. I am doing regex
on web pages and spacing problems have been a nightmare.
Here is another case in point - there was an extra
space after the end of the period which made my
original expression incorrect.



-----Original Message-----
customer = Regex.Replace(customer,"\\.$","");
or
customer = Regex.Replace(customer,@"\.$","");

Should both work fine. Your version ("[.]$") is considered "bad style", but
should nevertheless work. Post a short sample if it doesn't.

Niki

Phil396 said:
I am trying to mix metacharacters and the literal versions
in an expression and no matter what I try it does not
work.

Here is the code

customer = Regex.Replace(customer,"[.]$","");

I want to remove all periods from the end of customer
names. However I really need to know how to
mix metacharacters and the literal versions ( mostly
periods ) in an expression. I have tried using \.
@ and other ways to escape out the period but with
zero success.


.
 
Back
Top