jEdit Community - Resources for users of the jEdit Text Editor
Search and Replace EVERY 2nd occurrence i.e. alternate occurrences
Submitted by highab on Thursday, 3 February, 2011 - 10:45
Hi, I'd like to be able to search and replace paragraph marks (\n) with tab marks (\t) for every other line down to the end of a document, effectively merging two lines into one with a tab in between.

thus turning

term1
term2
something else 1
something else 2

into

term1[tab]term2
something else 1[tab]something else 2

this is the code I have so far

SearchAndReplace.setSearchString("\\n");
SearchAndReplace.setReplaceString("\\t");
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setIgnoreCase(true);
SearchAndReplace.setRegexp(true);
SearchAndReplace.replace(view);
SearchAndReplace.setSearchString("\\n");
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setIgnoreCase(true);
SearchAndReplace.setRegexp(true);
SearchAndReplace.find(view);

but if I put it into a loop for textarea number of lines, it replaces ALL occurrences of \n and not every other one.... I'm stumped.

Anyone have any ideas please?
Comment viewing options
Select your preferred way to display the comments and click 'Save settings' to activate your changes.
find
by Robert Schwenn on Thu, 03/02/2011 - 17:42
Hi,
the replace() method doesn't do anything unless some text has been found before. So, Your first loop only find an occurrence and the second loop replaces it and finds the next one...

Try this:
SearchAndReplace.setSearchString("\\n");
SearchAndReplace.setReplaceString("\\t");
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setIgnoreCase(true);
SearchAndReplace.setRegexp(true);
SearchAndReplace.find(view);
SearchAndReplace.find(view);
SearchAndReplace.replace(view);

Robert
 
found it!
by highab on Fri, 04/02/2011 - 09:34
I'd tried all sorts of things like that actually... in longhand, so its nice to see that the shorthand way IS possible.

The missing bit was

SearchAndReplace.setAutoWrapAround(false);

probably due to my loop grammar not being great, but hey, I've got it working now... Thanks!
User login
Browse archives
« November 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   101634
Context Free Art (*.cfdg)   0.31   46062
BBEdit scheme   1.0   18601
JBuilder scheme   .001   18502
ColdFusion scheme   1.0   18031
R Edit Mode - extensive version   0.1   17481
Advanced HTML edit mode   1.0   16213
Matlab Edit Mode   1.0   16075
jEdit XP icons   1.0   15236
XP icons for jEdit   1.1   14300