Giter Site home page Giter Site logo

Comments (2)

jacobslusser avatar jacobslusser commented on September 13, 2024

In ReplaceTargetRe your search string can include a backreference to one of the regular expression capture groups (called "tags" in Scintilla) from the previous search. This is helpful if you want your replacement string to include a portion of the matched text. For example, I use ReplaceTargetRe when I want to convert a bunch of C++ defines from the Scintilla.h header file into C# constants. Take the following text for example:

SCI_SETCARETWIDTH 2188
SCI_GETCARETWIDTH 2189
SCI_SETTARGETSTART 2190
SCI_GETTARGETSTART 2191
SCI_SETTARGETEND 2192
SCI_GETTARGETEND 2193
SCI_SETTARGETRANGE 2686
SCI_GETTARGETTEXT 2687

If I wanted to convert these to C# constants I would need to prefix each line with public const int, insert an = (equals) character between the variable and value, and finish off the line with a ; (semicolon). Said another way, what we want to do is search for "<variable> <value>" and replace it with "public const int <variable> = <value>;". That would be impossible to do with simple string replacement because the variable names and values are different for each match. But with a clever regular expression and the ReplaceTargetRe method it's quite easy. Here's how:

scintilla.TargetWholeDocument();
scintilla.SearchFlags = (SearchFlags.Regex | SearchFlags.Posix);

while (scintilla.SearchInTarget(@"([_A-Z]+)[ ]+([0-9]+)") >= 0)
{
    scintilla.ReplaceTargetRe(@"public const int \1 = \2;");
    scintilla.SetTargetRange(scintilla.TargetEnd, scintilla.TextLength);
}

My regular expression looks for any '_' (underscore) and 'A-Z' characters to find the variable name, then one or more ' ' (space) characters, followed by one or more '0-9' characters. Surrounding the sequences that match the variable name and value I've placed parentheses. These are called capture groups and/or backreferences in most Regex flavors and tags in Scintilla. I use all the terms interchangeably. Counting in regular expressions start with 1. So the first group, the variable name is backreference 1. The second group, the value, is backreference 2. etc...

By using the macro values \1 and \2 in my replace pattern I'm telling Scintilla to substitute what it finds for ([_A-Z]+) and ([0-9]+), respectively, into the replacement string. The result is that for each search/replace, the pattern "public const int \1 = \2;" will be expanded to:

public const int SCI_SETCARETWIDTH = 2188;
public const int SCI_GETCARETWIDTH = 2189;
public const int SCI_SETTARGETSTART = 2190;
public const int SCI_GETTARGETSTART = 2191;
public const int SCI_SETTARGETEND = 2192;
public const int SCI_GETTARGETEND = 2193;
public const int SCI_SETTARGETRANGE = 2686;
public const int SCI_GETTARGETTEXT = 2687;

The GetTag method is a companion to ReplaceTargetRe and allows you to get the text of each matched capture group if you wanted to manually construct the replacement string. For example, the same code above could be written like this using ReplaceTarget and GetTag:

scintilla.TargetWholeDocument();
scintilla.SearchFlags = (SearchFlags.Regex | SearchFlags.Posix);

while (scintilla.SearchInTarget(@"([_A-Z]+)[ ]+([0-9]+)") >= 0)
{
    scintilla.ReplaceTarget(@"public const int " + scintilla.GetTag(1) + " = " + scintilla.GetTag(2) + ";");
    scintilla.SetTargetRange(scintilla.TargetEnd, scintilla.TextLength);
}

Doing this obviously allows you greater control over the replacement string. I could, for example, change it to scintilla.GetTag(1).ToLower() if I wanted to change all the variable names to lowercase. That isn't possible in the automatic process used by ReplaceTargetRe but is if you wanted to construct your own replacement string with GetTag.

P.S. I bet you'll never look at the Replace dialog in Notepad++ the same way again. ;)

from scintillanet.

theryan722 avatar theryan722 commented on September 13, 2024

Not only have you answered my question, you also explained it extremely well and in depth. My oh my is this repository the jackpot. Thank you for everything.

from scintillanet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.