Regex - replacementpattern is ignored

Hi,
A filter/replace routine is not working. My regex is being correctly recognized(regexmatch variable is set to 1) but the replacement pattern is not applied, my code snippet is as follows :


  Dim inputrow As string  
  Dim CSVsep As string
  Dim regex199x as RegEx
  Dim regex2xxx as RegEx
  Dim regex19check as RegExMatch
  Dim regex2kcheck as RegExMatch

  'Initialize variables  
  csvsep = ";"
  regex199x = new RegEx
  regex2xxx = new RegEx
  regex199x.searchpattern = "(\\s)(199\\d/)"
  regex199x.Options.ReplaceAllMatches = False
  regex2xxx.searchpattern = "(\\s)(20\\d\\d/)" 
  regex2xxx.Options.ReplaceAllMatches = False

  'Prepare CSV separator strings
  regex199x.ReplacementPattern = CSVsep + "\\2"
  regex2xxx.ReplacementPattern = CSVsep + "\\2"

  do
    'Read line from file
    s=tis.ReadLine
    'Check if line is empty
    if len(s) > 2 then      
      inputrow = s
      'Remove trailing spaces
      inputrow = trim(inputrow)      
       regex19check = regex199x.search(inputrow)
       regex2kcheck = regex2xxx.search(inputrow)
       if regex19check <> nil then
          inputrow = regex199x.replace (inputrow)
        elseif regex2kcheck <> nil then
          inputrow = regex199x.replace (inputrow)
        end if     

I need to check one line to see if a year (199x/ or 20xx/) is present and change that block to (;199x/ or ;20xx/)

Thank you,
Matteo

Try doing the replace without the initial search and see what happens.

No joy, removing the regex.search passage doesn’t change a thing :frowning:

Can you post some actual sample data? I’d like to try this here.

Also, try “$2” instead of “\2” in the replacement pattern.

Here is a sample of the text I need to format


                        11127 2014/11/23 09:26:08	d:\\downloads\\FirefoxPortable\\Data\\profile\\pluginreg.dat
	  		    3261 2014/11/23 09:26:31	d:\\downloads\\FirefoxPortable\\Data\\profile\\prefs.js
	  		   19768 2014/11/23 09:28:00	d:\\downloads\\FirefoxPortable\\Data\\profile\\sessionstore.js
	  		      32 2014/11/23 09:26:03	d:\\downloads\\FirefoxPortable\\Data\\profile\\urlclassifier.pset
	  		15728640 2014/11/23 09:26:03	d:\\downloads\\FirefoxPortable\\Data\\profile\\urlclassifier3.sqlite

Switching the replacementpattern to $2 didn’t help. I can share the project over pm if you like :slight_smile:

Yes, that would help, please do.

[code]
//
// Note the change from “regex199x” to “regex2xxx” below.
//
// elseif regex2kcheck <> nil then
// inputrow = regex199x.replace (inputrow)
// end if
//
Dim inputrow As string
Dim CSVsep As string
Dim regex199x as RegEx
Dim regex2xxx as RegEx
Dim regex19check as RegExMatch
Dim regex2kcheck as RegExMatch

Dim tis As TextInputStream
Dim f As FolderItem
Dim s As String
Dim strFileName As String

'Initialize variables
csvsep = “;”
regex199x = new RegEx
regex2xxx = new RegEx
regex199x.searchpattern = “(\s)(199\d/)”
regex199x.Options.ReplaceAllMatches = False
regex2xxx.searchpattern = “(\s)(20\d\d/)”
regex2xxx.Options.ReplaceAllMatches = False

'Prepare CSV separator strings
regex199x.ReplacementPattern = CSVsep + “\2”
regex2xxx.ReplacementPattern = CSVsep + “\2”

Dim dlg as OpenDialog
dlg=New OpenDialog
dlg.InitialDirectory=GetFolderItem("")
dlg.SuggestedFileName=""
dlg.Title=“Select a file”
dlg.Filter = “”
’ Display Open dialog box
f=dlg.ShowModal()
If f<> Nil Then
strFileName = dlg.Result.AbsolutePath
// Input file
f=GetFolderItem(strFileName)
If f <> Nil And f.Exists = True Then
strFileName = dlg.Result.AbsolutePath
tis = TextInputStream.Open(f)
do
'Read line from file
s=tis.ReadLine
'Check if line is empty
if len(s) > 2 then
inputrow = s
'Remove trailing spaces
inputrow = trim(inputrow)
regex19check = regex199x.search(inputrow)
regex2kcheck = regex2xxx.search(inputrow)
if regex19check <> nil then
inputrow = regex199x.replace (inputrow)
elseif regex2kcheck <> nil then
inputrow = regex2xxx.replace (inputrow)
end if
MsgBox( "Input: " + s + EndOfLine + "Modified: " + inputrow)
End If
Loop Until tis.EOF
tis.CLose
End If
End If[/code]

It comes down to a typo. You are using the 19xx regex to perform the replacement in both cases.

Crikey i didn’t notice while debugging XD. Thank you everybody!
Before closing down the thread, does merging the two regex search pattern into this combo

dateregex.searchpattern = "(\\s)(199\\d/|20\\d\\d/)" dateregex.replacepattern = CSVsep + "\\2"

Would be equivalent to my existing code? This way i would be able to trim down my code a bit.

Yes.

BTW, “\s” represents any type of white space. You can use “( )” for the first subgroup or, if you want to be more exact, “(\x20)”.