RegEx "searchforward" replacement possible?

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
Alex Angelopoulos

RegEx "searchforward" replacement possible?

Post by Alex Angelopoulos »

This is the first time I've tried something like this, and Ihaven't a clue how to do it (or even if it is possible in TextPad).

I have several script subroutines which use an echo statement; I want to turn them all into functions which return a value instead of echoing it, which means as one step I need to look for

sub _somestring_
then search forward from there for the first following line which has the string
wscript.echo
and replace that second string with this:
_somestring_ =

Here's an example - note that I'm not worrying about the "sub" word itself on this pass, that part is easy.

BEFORE SUBSTITUTION

''''''''''''''''''''''''''''''''''
Sub DispMinPassLength(strDomain)
Dim Computer
Set Computer= GetObject("WinNT://" & strDomain)
wscript.echo Computer.MinPasswordLength
End Sub
''''''''''''''''''''''''''''''''

AFTER SUBSTITUTION

''''''''''''''''''''''''''''''''
Sub DispMinPassLength(strDomain)
Dim Computer
Set Computer= GetObject("WinNT://" & strDomain)
DispMinPassLength = Computer.MinPasswordLength
End Sub
''''''''''''''''''''''''''''''''

and then I can do a pass to swap the word "Sub" into "Function".

Any way to do this? I think it's called "anchoring"....
Roy Beatty

Anchors Aweigh

Post by Roy Beatty »

Find....: ^wscript\.echo \([^.]+\)\.\([[:graphic:]]+\)[ \t]+$<br>
Replace: Disp\2 = \1.\2

Explanation: The Find's first ^ anchors the expression to the start of a line. The $ anchors it to the end of a line. This means that for a match to occur, the entire line must match the Find pattern, a very useful and reliable way to design your regular expressions. The second ^ falls inside brackets -- in which case it means "any but the following character."

I just noticed that the subroutine name does NOT match the variable name (\2) and the intepreter/compiler probably prohibits making them the same(?). Even so, it would be nice if all of the return objects were named <prefix>subroutinename. If they aren't necessarily identical, then you have a more difficult set of string changes. You can't easily develop regex for multi-line patterns -- consider using a macro.

HTH,
Roy
Alex Angelopoulos

Re: Anchors Aweigh

Post by Alex Angelopoulos »

I skipped the other step since it is trivial - changing "sub" to "function".

I'm running through many, many lines of VBScript, which were all done as nifty demos with wscript.echo and response.write statements littered through the code. At least they were modularized, but the hard-coded echoing makes it difficult to reuse them. The regex idea was to do a major rehack on it, leaving me with only going through and verifying that each function works correctly alone.

From what you say (and other things I read today), I am somewhat tempted to skip to plan "b" - writing the regexes in VBScript or Jscript and then using the tools menu to run them as plugins. The macros are useful if you just want to do one thing one way, but if I have to reuse something in new ways, I want a little more flexibility. At least the autosave and prompted reload make it possible to pass that out...

Your regex expression is actually the handiest thing - I'm still getting used to using them correctly, and seeing how other people do complex ones is invaluable.
Post Reply