Comments (2)
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.
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)
- Changing EOL type crashes Scintilla HOT 4
- Help!!! How to setting color of default text HOT 2
- Avalonia UI support HOT 1
- Help! Cannot find a mask that works for RgbaImage marker symvol HOT 1
- How do i remove this corner? HOT 1
- How does line indentation work? HOT 1
- Question Regarding Combined Characters and Regex
- Failed to create component 'Scintilla' HOT 4
- sign project
- How can I remove the background color of the focused line? HOT 2
- Is there any way to highlight BETWEEN characters?
- Scrollbar don't work
- Scintilla Control causes caret in other controls to jump/bug HOT 1
- STOP drag drop on windows
- Lexer.Html does not support code folding
- Error on HEREDOC php HOT 3
- Cant drag ScintillaNET control into the form.
- Undo and Redo HOT 1
- .Lines[intIndex].Position always returns 0. - Latest version, not 3.6 release.
- Find in files feature
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scintillanet.