: Use GREP to replace multiple characters I need to replace a XXXXXX sequence in my document with the same number of underscore characters. I'm using (Y{2,15}) as the find argument, but can´t
I need to replace a XXXXXX sequence in my document with the same number of underscore characters. I'm using (Y{2,15}) as the find argument, but can´t find out what is the correct code for the replace argument.
More posts by @Yeniel278
3 Comments
Sorted by latest first Latest Oldest Best
Use a GREP Style. A GREP Style has the advantages that it is not destructive (it does not change your document contents), it is live (the style will automatically be removed and applied while you are changing your text), and when set up correctly it uses the exact same width as the original text (so there is no chance of unexpectedly changing your text flow).
An additional advantage is that you also can change the appearance of the underline itself: you can change its color, make it thinner or thicker, move it higher or lower, and make it a double, dashed, or dotted line. To do so, you can change the Underline Options in the character style.
Create a character style "Underline". Set its character color to 'None', switch Underline to On, and its color to Black (as the default is 'text color').
Add a GREP Style to your base text style that applies the character style to the match sequence (?<!X)X{2,15}(?!X).
Done.
The GREP match ensures that only an exact sequence of 2 to 15 X's in a row change to an underline. This exact match ensures that a sequence of 16 X's will not change to 15 underlines plus a single "X"; they will all remain unchanged. Also, using your original GREP, a sequence of 17 X's will be changed – first 15 in a row, then 2 in the next match. Again, my more exact match will not change such a sequence.
Indeed a script can do it:
//Main routine
var main = function() {
//==================VARS==================//
var
//The document
doc = app.properties.activeDocument,
//Find Grep Preferences in their current state
fgp = app.findGrepPreferences.properties,
//variables used for storing grep find values and items
found, n, text, count,
//A simple "underscores" string to replace found items contents
underscores = "___________________________________________________________________________________________________________";
//==================INITIAL CHECK==================//
if ( !doc ) return;
//==================SETTING GREP PROPS==================//
app.findGrepPreferences.properties = {
findWhat:"X{2,15}"
}
//==================FINDING STRINGS==================//
found = doc.findGrep();
n = found.length;
count = n;
//==================EXIT IF NO RESULTS==================//
if ( !n ) {
alert("No results sorry !");
return;
}
//==================REPLACING STRINGS==================//
while ( n-- ) {
text = found[n];
text.contents = underscores.substr (1, text.length);
}
//==================RESTORING GREP PREFS==================//
app.findGrepPreferences.properties = fgp;
//==================ALERT AND EXIT==================//
alert( count+" strings replaced" );
}
var u;
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
[
Not possible, however you can do it in several passes each replacing one character. You have to run the search and replace 15 times though. Might as well do this with scripting.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.