-2

The first character on every line of a file I have is a comma. How can I remove just this comma?

I have tried to use the replace method but it doesn't seem to accept special characters. Here is an example:

            myRegExp.Pattern = "\n,"
            strText5 =myRegExp.Replace(strText4,"\n")

The above snipper replaces the first new line char and comma with \n. How can I replace with a special character instead of a literal string?

The MSDN library doesn't seem to have the answers I need.

TIA.

Teezee
  • 35
  • 3

1 Answers1

0

If you enable MultiLine mode (and Global if you have not done so) then ^ will match the start of a line:

myRegExp.Pattern = "^,"
myRegExp.Multiline = true
myRegExp.Global = true

strText5 = myRegExp.Replace(strText4, "")

(In a vanilla VB string there are no escape sequences, "\n" is just the two slash+n characters, for a \n you would use vbLf or chr(10))

Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • Yeah doesn't surprise me @Alex answering another obvious duplicate question. – user692942 Apr 19 '17 at 18:32
  • The flagged duplicate isn't what I would have picked, but this is far from the first time someone has asked how to do a regex replace in VBScript. – user692942 Apr 19 '17 at 18:34
  • Also `\n` is new line so you should use `vbNewLine` to avoid architecture differences. In Windows for example `\n` is equivalent to `vbCrLf` not `vbLf`. – user692942 Apr 19 '17 at 18:36