Replace using Regular Expressions

  • Thread starter Thread starter Vlado B.
  • Start date Start date
V

Vlado B.

Hi everyone
I have tried something like this

Regex.Replace(theString, @"[.]+", "", RegexOptions.IgnorePatternWhitespace);

in other words delete every occurrence of one or more dots in some string.
It does not work.
Does anyone know what am I doing wrong?

TIA
Vlado

P.S. I also tried the expression in Expresso and it works there but I do not
know how to take code for replace from it.
 
To simple delete all dots in a String, you might find it easier using the
String.Replace method:
string result = theString.Replace(".", string.Empty);
 
Vlado B. said:
Hi everyone
I have tried something like this

Regex.Replace(theString, @"[.]+", "", RegexOptions.IgnorePatternWhitespace);

in other words delete every occurrence of one or more dots in some string.
It does not work.
Does anyone know what am I doing wrong?

@"\.*"?

"." without the escape matches any character.
 
you mean
theString = Regex.Replace(theString, @"[.]+", "",
RegexOptions.IgnorePatternWhitespace);

right?
;-)
 
No, I ment what I wrote and I admit I was wrong.
I don't know why have I thought that Regex.Replace automatically changes the
given string!?

Thank you very much, now it works just perfect.

Vlado


Uri Dor said:
you mean
theString = Regex.Replace(theString, @"[.]+", "",
RegexOptions.IgnorePatternWhitespace);

right?
;-)
Hi everyone
I have tried something like this

Regex.Replace(theString, @"[.]+", "", RegexOptions.IgnorePatternWhitespace);

in other words delete every occurrence of one or more dots in some string.
It does not work.
Does anyone know what am I doing wrong?

TIA
Vlado

P.S. I also tried the expression in Expresso and it works there but I do not
know how to take code for replace from it.
 
since RegEx.Replace doesn't modify the string but instead returns a new one
you have to:

theString = Regex.Replace(theString, @"[.]+", "",
RegexOptions.IgnorePatternWhitespace);
 
Yeah,I agree.This just because it is more efficient to use the
String.Replace method.

Dennis Myrén said:
To simple delete all dots in a String, you might find it easier using the
String.Replace method:
string result = theString.Replace(".", string.Empty);

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Vlado B. said:
Hi everyone
I have tried something like this

Regex.Replace(theString, @"[.]+", "",
RegexOptions.IgnorePatternWhitespace);

in other words delete every occurrence of one or more dots in some string.
It does not work.
Does anyone know what am I doing wrong?

TIA
Vlado

P.S. I also tried the expression in Expresso and it works there but I do
not
know how to take code for replace from it.
 
Back
Top