• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Find and Replace: how to extract capture groups from the found text

Community Expert ,
Nov 24, 2022 Nov 24, 2022

Copy link to clipboard

Copied

@Fightergator asked a question, which I will handle in an own post here:

I have one questions related to a brief discussion we had a couple of months ago. I'm using the Find script snippet that has been posted here numerous times, and with it I can Find using wildcards and regular expressions, along with consider case. What I can't seem to figure out is how to extract capture groups from the found text and use them to customize the replacement string. Currently all I can do is insert a fixed (dumb) replString that I load along with the findString. From what I can see, your script has the ability to process capture groups.

I had the very same problem during the development of FMfindRepl. Hence the published version 0.80 diverted the  handling of Replacing something found be RegEx to the original FM panel. But then i had an idea which is implemented in the current versio 1.00.

In module FMfindRepl_PalF, function ButtonFind you see

518  oTR = KLD_F.FindSomething (sFType, sSearch, iMode, bWord, bCase, bBack);

iMode is 2 for searching by RegEx. oTR is the found text range.

In this stage we do not get any clue about the components found by the RegEx, hence in the script we have no idea for a replacement, such as found group $1.

Therefore I analyse the found text range with the same RegEx in the replacement function ButtonReplace

722   sResult = KLD_F.ReplaceInString (sText, sSearch, sReplace, bCase, bWord, bRegEx)

Function ReplaceInString is located in module FMfindRepl_Anicllary:

KLD_F.ReplaceInString = function (string, sSearch, sReplace, bCase, bWord, bRegEx) {
/*            Find substring with options and replae it
Arguments     string          String in which to search
              sSearch         String to be searched
              sReplace        String replacing the search
              bCase           true for case sentitive search
              bWord           true so search whole words only
              bRegEx          true to interprete sSearch as RegEx
Returns       The string with the replacement
Called by     ButtonReplace
History       2021-10-18
*/
var eReg0, eReg1, eReg2, eReg2, eReg3, eRegR, sResult, sFind;

  if (string === undefined) { string = ""; }
  if (!bRegEx) {                        // escape the 12 syntactical chars
    sFind = sSearch.replace (/([\\\^\$\.\|\?\*\+\(\)\[\{])/g,"\\$1"); 
  }
  eReg0 = new RegExp (sFind, "i");            // case insesitive, no words
  eReg1 = new RegExp (sFind);                 // case sensitive, no words
  eReg2 = new RegExp (sFind + "\\b", "i");    // case insesitive, word
  eReg3 = new RegExp (sFind + "\\b");         // case sensitive, word
  if (bRegEx) {
    eRegR = new RegExp (sSearch);             // interprete as RegEx
    sResult = string.replace (eRegR, sReplace);
  } else {
    if (!bCase && !bWord) {
      sResult = string.replace (eReg0, sReplace);
    } else if (bCase && !bWord) {
      sResult = string.replace (eReg1, sReplace);
    } else if (!bCase && bWord) {
      sResult = string.replace (eReg2, sReplace);
    } else {
      sResult = string.replace (eReg3, sReplace);
    }
  }
  return sResult;
} //--- end ReplaceInString

 

TOPICS
Scripting

Views

130

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 25, 2022 Nov 25, 2022

Copy link to clipboard

Copied

LATEST

Many thanks for this.  After downloading your FMfindRepl 1.0 and doing a quick scan through the scripts I noticed this module and thought it could be related to the capture groups.  It will take a few days for me to study it.  I'll likely have more questions, but don't want to waste your time until I know more.   

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines