jEdit Community - Resources for users of the jEdit Text Editor
How to use SearchAndReplace in a loop?
Submitted by tvojeho on Wednesday, 21 November, 2007 - 16:59
Hello,

I have been using jEdit for a few months now, and it is an awesome editor. I have written a few macros, but I do not know Beanshell very well and now I am stumped.

I want to write a macro to correct some words using SearchAndReplace command. I would like to be able to define the words to be searched and replaced, and then to run a loop, where the replacements happen. I am vaguely aware of things such as arrays and maps, and I think in my case I am going to have to use one of those, but I don't know where to begin.

This is an example of how I want it to work (just a concept, not a working code):

//Define search and replace pairs
searchReplacePairs[] = ("thats","that's",
"lets","let's"
"...","...");

//Grab each pair and perform replace
Loop searchReplacePairs
{
SearchAndReplace.setSearchString(textToSearch);
SearchAndReplace.setReplaceString(textToReplace);
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setIgnoreCase(true);
SearchAndReplace.setRegexp(true);
SearchAndReplace.setSearchFileSet(new CurrentBufferSet());
SearchAndReplace.replaceAll(view);
}

As you can see, I do not know how to define the array, then how to parse it to get the textToSearch/textToReplace pair and process it in the loop. Any help would be welcome.

Thanks, tvojeho
Comment viewing options
Select your preferred way to display the comments and click 'Save settings' to activate your changes.
Hi, if any newbie is intere
by tvojeho on Sun, 25/11/2007 - 14:20
Hi,
if any newbie is interested, I have managed to somehow patch the replacement loop together. It is fairly inelegant looking, but it works for me:

void runReplaceLoop(){
searchReplacePairs_count = searchReplacePairs.length;
for (int i = 0; i < searchReplacePairs_count; i++) {
textToSearch = searchReplacePairs[i];
i++;
textToReplace = searchReplacePairs[i];
i++;
if(searchReplacePairs[i]!="")
{
Regex_flag = "true";
SearchAndReplace.setRegexp(true);
} else {
Regex_flag = "false";
SearchAndReplace.setRegexp(false);
}
i++;
if(searchReplacePairs[i]!="")
{
IgnoreCase_flag = "true";
SearchAndReplace.setIgnoreCase(true);
} else {
IgnoreCase_flag = "false";
SearchAndReplace.setIgnoreCase(false);
}

// Replacement
SearchAndReplace.setSearchString(textToSearch);
SearchAndReplace.setReplaceString(textToReplace);
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setSearchFileSet(new CurrentBufferSet());
SearchAndReplace.replaceAll(view);
}
}

String[] searchReplacePairs = {
"¶0¶","¶@048@¶","RE","IC",
"([^\\d\\-\\s])(\\s*)0(\\s*)([a-zŠŤŽšťžá-ý])","$1$2¦$3$4","RE","IC",
"([^\\d\\s])(\\s*)0([a-zŠŤŽšťžá-ý])","$1$2¦$3","RE","IC",
"([^\\d\\-\\s])\\s*¦¶([a-zŠŤŽšťžá-ý])","$1 ¦$2","RE","IC",
"([^\\d\\s])\\s*¦\\s+([a-zŠŤŽšťžá-ý])","$1 ¦$2","RE","IC",
};
runReplaceLoop();

Regards, tvojeho
 
Sear and Replace and Loop
by pgmagallanes on Sat, 04/07/2009 - 19:37
I am a newbie. I have saved the code to JEdit and I get the following I/O error: Cannot save: java.io.CharConversionException: Failed to encode the character "T ?" (U+164) at column 35 in line 37 with the encoding "Cp1252"

I don't know how to print the character (U+164). Do I need to import a file before this macro will work?

Thanks, Phil
 
character encoding
by Robert Schwenn on Sat, 04/07/2009 - 20:13
There are uncommon characters in the regular expression like these: "ŠŤŽšťžá-ý". To save them to a file You have to chose an appropriate encoding via Utilities -> Buffer Options -> Character Encoding before.

If You didn't need these characters You should replace them simply by ordinary characters.

Robert
User login
Browse archives
« April 2024  
MoTuWeThFrSaSu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
Poll
Are you interested in language packs for jEdit?
Yes, and I could help maintain translations
26%
Yes, I'd like to have translations
32%
Indifferent
35%
No, that'd be bad (please comment)
7%
Total votes: 1093
Syndication
file   ver   dls
German Localization light   4.4.2.1   82348
Context Free Art (*.cfdg)   0.31   46055
JBuilder scheme   .001   18495
BBEdit scheme   1.0   18116
ColdFusion scheme   1.0   18024
R Edit Mode - extensive version   0.1   17473
Advanced HTML edit mode   1.0   16206
Matlab Edit Mode   1.0   16068
jEdit XP icons   1.0   15229
XP icons for jEdit   1.1   14293