Loading ...
Sorry, an error occurred while loading the content.

Revised Ad hoc Perl Regexp Replacement Clip

Expand Messages
  • Sheri
    Hi, I had moved some lines around and separated each of the Unit-type processes into separate labels, and lost some required lines for Para (thanks Alan, for
    Message 1 of 1 , Jan 16, 2003
    • 0 Attachment

      I had moved some lines around and separated each of the Unit-type processes into separate labels, and lost some required lines for "Para" (thanks Alan, for pointing it out). This version fixes that problem, and also preserves all the chosen values as default criteria for the next run during the session with the following exceptions:

      If the previous criteria is inconsistent with the presence or absence of selected text in the document, it will default to "Highlight" or "Down" instead.

      To avoid misinterpretation of prior find and replace texts these values are altered or cleared under certain circumstances. If one of the texts starts with an equal sign, it is changed to "\=". An unmatched "}" in either text causes the prior text to be cleared. You can purposely retrieve such a value in the file PerlRepl.pl in the Notetab Scripts directory prior to a rerun.

      The wizards for collecting Search and Replace texts were changed to wrapped memo fields to avoid conflicts with the "|" character. If you insert hard breaks in these fields (by pressing the Enter key) these get converted to "\n"'s.

      I think (hope) all is well now, but let me know if you have a problem.

      There are many similarities between Notetab's regexp and Perl regexp (e.g., patterns, the need to escape certain characters with a "\", etc.). A few major differences are:

      *Tagged matches in the Search text go in parentheses not curly braces.

      *Tagged matches in the Replace text are referenced with a dollar sign, not a "\". For example $1 not \1

      *Curly braces are used in the Replace text around a specified number of occurences. For example:
      means 12 d's.


      This clip can be viewed and copied without broken lines around:


      H="Regexp Replace Using Perl"
      ;2003-01-13 by Sheri Pierce
      ;2003-01-16 fixed "Para" processing
      ;2003-01-16 enhanced to present all previously set values as defaults on future runs (except default to "Highlight" if there is Highlighted text and down if previously used was "Highlight" but now there is no selected text)
      ;2003-01-16 added conditional processing to help prevent clip wizard from getting wrong signals from prior search and replace texts (e.g., variable used "{" "}" or "=" character); if necessary the prior text is cleared to avoid the problem.
      ;2003-01-16 Changed Search and Replace text wizards to wrapped memos to avoid conflicts with the "|" character; be warned that hard breaks are converted to "\n".
      ;Use reload or undo if you don't get desired results in your file.
      ;Be warned that undo isn't available if the file is large-ish.
      ^!Close "^$GetScriptPath$PerlRepl.pl" Save
      ^!Set %Curdoc%=^$GetDocIndex$
      ^!Set %ScopeD%="^%Scope%"
      ^!Set %GlobalD%="^%Global%"
      ^!Set %CaseID%="^%CaseI%"
      ^!Set %UnitD%="^%Unit%"

      ^!IfTrue ^$IsEmpty(^%ScopeD%)$ Next Else Skip_4
      ^!Set %ScopeD%="Down"
      ^!Set %GlobalD%="g"
      ^!Set %CaseID%="i"
      ^!Set %UnitD%="Lines"

      ^!If ^$GetSelSize$>0 ^!Set %ScopeD%="Highlight"
      ^!If ^$GetSelSize$=0 Next Else Skip_1
      ^!If ^%ScopeD%="Highlight" ^!Set %ScopeD%="Down"

      ^!Set %ScopeX%="^$SetDefaultValue(Down|Highlight|All;^%ScopeD%)$"
      ^!Set %UnitX%="^$SetDefaultValue(Lines|Paragraphs delimitted by Double Space^=Para|Entire File or Highlight (or Cursor-down area) as one Unit^=File;^%UnitD%)$"
      ^!IfTrue ^$IsEmpty(^%GlobalD%)$ Next Else Skip_2
      ^!Set %GlobalX%="Global^=g|"
      ^!Goto Skip_1
      ^!Set %GlobalX%="_Global^=g|"
      ^!IfTrue ^$IsEmpty(^%CaseID%)$ Next Else Skip_2
      ^!Set %CaseIX%="Case Insensitive^=i|"
      ^!Goto Skip_1
      ^!Set %CaseIX%="_Case Insensitive^=i|"

      ^!Set %FT%="^?{(T=W)Find Pattern=^%FT%}";%RT%="^?{(T=W)Replace Pattern=^%RT%}";%Scope%=^?{(T=C)Scope==^%ScopeX%};%Global%=^?{(T=A)Options=^%GlobalX%};%CaseI%=^?{(T=A)=^%CaseIX%};%Unit%="^?{(T=L)Units are==^%UnitX%}"
      ^!IfCancel EndIt
      ^!Set %FT%=^$StrReplace("^%NL%";"\n";"^%FT%";No;No)$
      ^!Set %RT%=^$StrReplace("^%NL%";"\n";"^%RT%";No;No)$
      ^!IfTrue ^$IsEmpty(^%FT%)$ Next Else Skip_2
      ^!Prompt Find Text cannot be empty.
      ^!Goto EndIt
      ^!IfDiff "^%Scope%" "Highlight" Next Else Skip_1
      ^!If ^$GetSelSize$>0 MsgHighlight
      ^!IfSame "^%Scope%" "Highlight" Next Else Skip_2
      ^!If ^$GetSelSize$=0 Next Else Skip_1
      ^!Set %WarnText%="Warning: Selected "Highlight" but no Selected Text - Entire File^%NL%will be Highlighted. Cancel if this is not what you want.^%NL%^%NL%"
      ^!Prompt ^%WarnText%Verify Criteria:^%NL%^$StrFill("=";16)$^%NL%Scope: ^%Scope%^%NL%Unit: ^%Unit%^%NL%^%NL%FindText = ^%FT%^%NL%^%NL%ReplaceText = ^%RT%^%NL%Options = ^%Global%^%CaseI%^%NL%
      ^!Goto Scope
      ^!IfSame "^%Scope%" "All" Next Else Skip_1
      ^!Select 0
      ^!IfSame "^%Scope%" "Down" Next Else Skip_3
      ^!Set %Pos%=^$GetRow$:^$GetColStart$
      ^!Jump Doc_End
      ^!SelectTo ^%Pos%
      ^!SetScreenUpdate Off
      ^!IfFileExist "^$GetScriptPath$PerlRepl.pl" Next ELSE MakeInitFile

      ^!Open "^$GetScriptPath$PerlRepl.pl"
      ^!Toolbar Clear All
      ^!Goto ^%Unit%

      ^!InsertText $/="";
      ^!InsertText ^P
      ^!InsertText while (<>) {
      ^!InsertCode s/^%FT%/^%RT%/^%Global%^%CaseI%;
      ^!InsertText ^P
      ^!InsertText print;
      ^!InsertText }
      ^!InsertText ^P
      ^!Goto After

      ^!Insertcode $text = do { local $/; <> };
      ^!InsertText ^P
      ^!Insertcode $text =~
      ^!InsertCode s/^%FT%/^%RT%/^%Global%^%CaseI%;
      ^!InsertText ^P
      ^!InsertText print $text;
      ^!InsertText ^P
      ^!Goto After

      ^!InsertText while (<>) {
      ^!InsertText ^P
      ^!InsertCode s/^%FT%/^%RT%/^%Global%^%CaseI%;
      ^!InsertText ^P
      ^!InsertText print;
      ^!InsertText ^P
      ^!InsertText }
      ^!InsertText ^P
      ^!Goto After

      ^!Close Save
      ^!SetDocIndex ^%Curdoc%

      ^!SetWizardLabel Your criteria has been saved in PerlRepl.pl. Run Perl?
      ^!IfTrue ^$IsEmpty(^%WarnText%)$ Next Else Skip_3
      ^!If ^?{(T=L)Continue==_Yes|No}="Yes" Next Else EndIt
      ^!IfCancel EndIt
      ^!Goto RunPerl
      ^!If ^?{(T=L)Continue==Yes|_No}="Yes" Next Else EndIt
      ^!IfCancel EndIt

      ^!RunPerl "^$GetScriptPath$PerlRepl.pl"
      ^!IfDiff "^%Scope%" "Highlight" Next Else Skip_1
      ^!Jump Select_Start
      ^!SetScreenupdate on
      ^!Goto EndIt

      ^!TextToFile "^$GetScriptPath$PerlRepl.pl" file created
      ^!IfFileExist "^$GetScriptPath$PerlRepl.pl" CreatePerl
      ^!Goto Skip_-1
      ^!Info Ambiguous Scope: Highlighted text exists but scope = ^%Scope%
      ^!Set %Scope%=^?{Use==_Use ^%Scope%^=^%Scope%|Use Highlight^=Highlight}
      ^!Goto PostMsg
      ^!ClearVariable %ScopeD%
      ^!ClearVariable %GlobalD%
      ^!ClearVariable %CaseID%
      ^!ClearVariable %UnitD%
      ^!ClearVariable %ScopeX%
      ^!ClearVariable %GlobalX%
      ^!ClearVariable %CaseIX%
      ^!ClearVariable %UnitX%
      ^!ClearVariable %WarnText%
      ^!If ^$StrCount("}";"^%FT%";No;No)$>^$StrCount("{";"^%FT%";No;No)$ Next Else Skip_1
      ^!ClearVariable %FT%
      ^!If ^$StrPos("}";"^%FT%";No)$=^$StrPos("{";"^%FT%";No)$ Skip_2 Else Next
      ^!If ^$StrPos("}";"^%FT%";No)$<^$StrPos("{";"^%FT%";No)$ Next Else Skip_1
      ^!ClearVariable %FT%
      ^!If ^$StrCount("}";"^%RT%";No;No)$>^$StrCount("{";"^%RT%";No;No)$ Next Else Skip_1
      ^!ClearVariable %RT%
      ^!If ^$StrPos("}";"^%RT%";No)$=^$StrPos("{";"^%RT%";No)$ Skip_2 Else Next
      ^!If ^$StrPos("}";"^%RT%";No)$<^$StrPos("{";"^%RT%";No)$ Next Else Skip_1
      ^!ClearVariable %RT%
      ^!IfSame "^$StrCopyLeft("^%FT%";1)$" "=" Next Else Skip_1
      ^!Set %FT%=^$StrInsert("\";"^%FT%";1)$
      ^!IfSame "^$StrCopyLeft("^%RT%";1)$" "=" Next Else Skip_1
      ^!Set %RT%=^$StrInsert("\";"^%FT%";1)$
      ;end of clip
    Your message has been successfully submitted and would be delivered to recipients shortly.