viewpoint-particle

Author Topic: udfRegExpReplace() in binary buffer  (Read 59 times)

cssyphus

  • Newbie
  • *
  • Posts: 11
udfRegExpReplace() in binary buffer
« on: September 10, 2017, 05:05:02 pm »
I am programmatically modifying a large text file containing some HTML/JS code and need to "mark" locations where there is a space between two selectors, such as between an ID and className, as in $('#btnOne .subElement'). To find these, I need a RegEx. The following RegEx works well in Notepad++, and here in this jsFiddle (https://jsfiddle.net/m7pamjLp/1/), to identify substrings in which to replace the spaces.

>>>  \$\(['"].+ .*\)\.  <<<

The text (code) to be searched is approx 7500 lines, 250K, so wish to do this in a binary buffer. Also, the presence of the percent sign in the txtAdvisory string (above) breaks udfRegExpReplace(). I am hoping that using a binbuf will also fix that problem.

How would I write winbatch code to do a RegEx replace in a binary buffer?

I found Detlev's wonderful RegEx UDFs, but cannot think how to adapt udfRegExpReplace() to use binbufs. A little help?

td

  • Tech Support
  • *****
  • Posts: 2140
    • WinBatch
Re: udfRegExpReplace() in binary buffer
« Reply #1 on: September 11, 2017, 07:22:59 am »
There is no reason to use a binary buffer based on the information you have provided.  A file of 750K bytes is not that large and easily handled by the FileGet function.  Using a binary buffer would not save any computer memory and may even cause your script to use more (not that it would be an issue with a file in this size range.) 

The percent sign only causes issues when it is placed in a string literal and the way to avoid that is to escape it in that string literal, i.e., use '%%' instead of '%'.

FWIW, I prefer using the FCL's  "System.Text.RegularExpressions.Regex" class for regular expression processing in scripts because it has a more complete implementation than the COM Automation version.   However, the regex you want to use is not that complex so you likely don't need a full implementation.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


cssyphus

  • Newbie
  • *
  • Posts: 11
Re: udfRegExpReplace() in binary buffer
« Reply #2 on: September 11, 2017, 12:49:56 pm »
Thanks Tony.

(1) Is there an example somewhere of using the .NET System.Text.RegularExpressions.Regex in a WinBatch script?

(2) I'm not a .NET coder and not familiar with the FCL, so coming at this new. Yet I can see how useful it would be to have access to all the FCL's functions/classes. Is there a primer for using the FCL in general with WinBatch scripts? (Preferring examples to explanation)

td

  • Tech Support
  • *****
  • Posts: 2140
    • WinBatch
Re: udfRegExpReplace() in binary buffer
« Reply #3 on: September 11, 2017, 01:23:00 pm »
There is a whole section of examples of WinBatch CLR hosting in the Tech Database.  It includes several using the "System.Text.RegularExpressions.Regex" class.  Of course, the three WILL functions ObjectClrNew,  ObjectClrOption, and ObjectClrType used to initiate and support CLR hosting are documented in the Consolidated WIL Help file.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates