Samfin
Samfin

Reputation: 3

VBScript Regular Expression Insert Line

I try to insert a line between 2 specific lines in a files with VBScript :

# Ligne 1
# Ligne 2

=>

# Ligne 1
# Ligne 1 bis
# Ligne 2

My script code is :

Dim regEx
Set regEx = New RegExp   

regEx.Pattern = "# Ligne 1\r\n# Ligne 2"      
regEx.Pattern = patrn       
regEx.IgnoreCase = not Casse   
regEx.Global = True  

RegExpReplace=regEx.Replace(Source,"# Ligne 1\r\n# Ligne 1 bis\r\n# Ligne 2")

The expression has been found but the replacement text is bad. The result is :

# Ligne 1\r\n# Ligne 1 bis\r\n# Ligne 2

I tried :

regEx.Pattern = "^# Ligne 1$\r\n^# Ligne 2$"   
regEx.IgnoreCase = not Casse   
regEx.Global = True  
regEx.Multiline = True
RegExpReplace=regEx.Replace(Source,"^# Ligne 1$\r\n^# Ligne 1 bis$\r\n^# Ligne 2$")

The result is :

^# Ligne 1$\r\n^# Ligne 1 bis$\r\n^# Ligne 2$

Any idea ? Thanks

Upvotes: 0

Views: 112

Answers (1)

MC ND
MC ND

Reputation: 70923

RegExpReplace=regEx.Replace(Source,"# Ligne 1" & vbCrLf & " # Ligne 1 bis" & vbCrLf &"# Ligne 2")

You replace the string found matching the regular expression with the string indicated as replacement (with the exception of capture groups placeholders). Inside a VBScript \r\n has not the same meaning than in a regular expression, so, if you need to include a carriage return and line feed you need to directly concatenate them

Upvotes: 1

Related Questions