Find and replace
Page 1 of 1

Author:  boblines [ 2010-08-13 19:03:08 ]
Post subject:  Find and replace

I want to italicise all words or phrases in my document which are contained inside parentheses e.g. (dog) to become dog. Is there a simple way of doing this?

Author:  martin [ 2010-08-14 09:42:06 ]
Post subject:  Re: Find and replace

Hi Bob- this is definitely possible. Here's how:

1. Open up the Find & Replace window.
2. Switch the "Using" mode to "PowerFind Pro".
3. In the "Find What" field, type this magic:

4. Press the "Find All" button.

Now that you have all parenthesized text selected, you can switch back to the document window and use the menus/palettes to format all the text at once.

Oh, I just realized that you probably don't want to italicize the parentheses themselves, just the text between. So let's use this magic PowerFind Pro expression instead:

Author:  boblines [ 2010-08-14 11:32:06 ]
Post subject:  Re: Find and replace

Thanks Martin: works perfectly. Could you explain the code, please?


Author:  martin [ 2010-08-15 18:42:50 ]
Post subject:  Re: Find and replace

Sure thing Bob. Let's start with the simpler expression:

You may notice the backslashes ("\") in front of the parentheses. That's because parentheses have a special meaning in PowerFind Pro. They are called "captures" and usually aren't treated as literal characters to match unless we escape them with the backslashes. With the backslash "\(" just matches a single open parenthesis.

The middle part includes a period "." which is a wild card that matches any character (except a newline). The "+" following it is a repetition operator, and modifies the pattern that comes just before. It means that the preceding pattern should be matched one or more (eg: unlimited) times. So together ".+" means to match one or more of any character.

Normally the repetition operators will match as much as they can. For example, with the text "hello (there), and (goodbye)" the pattern "\(.+\)" would match "(there), and (goodbye)", because it's happy to gobble up as many characters as it can. For that reason they are called "greedy". Instead, we want the repetition to stop once any closing parenthesis is found. To do that we need to make the repetition "stingy", which can be done by following the plus with a "?".

Now, onto the more complicated expression:

That expression is actually very similar to the first one, it just uses something called look-behind and look-ahead. Those allow the matching of text, but without including it in the found result / selection. It's a way of saying, I need this to be there, but I don't want it selected. A look-behind is the ugly grouping "(?<=whatever)". You can see that instead of looking for "whatever" we want to match a literal parenthesis, so we drop in "\(". Similarly, look-ahead is specified by "(?=whatever)", where we drop in "\)".

Hopefully that makes some sense! Let me know if you have any questions.

Author:  boblines [ 2010-09-21 21:56:37 ]
Post subject:  Re: Find and replace

Some time ago one of you cleverr people helped me with a 'find and replace' problem and also sent me a lot of code which worked perfectly but I still fail to understand. :( Could you help again please? I want to remove underscores which embrace a word or phrase thus : _Dog_ and italicize Dog. . I have tried modifying the code you kindly sent last time, but without success. Is this language easily learned, what is it called, and where to I go to learn about it?

Thanks in advance

Bob Lines

Author:  Kino [ 2010-09-22 01:44:11 ]
Post subject:  Re: Find and replace

I think this would do the job.

1. Open the Find panel;
2. Set Using to PowerFind Pro;
3. In the Find What field, type this:
4. In the Replace with field, type this:
5. Hit the Replace All button (the underscores will be removed but words and phrases embraced by them will remain selected);
6. Activate the document window by closing the Find panel or by clicking on the document window’s Toolbar;
7. Hit command-i (Italic).

( . . . ) : captured group
[^_\s] : negative set matching any character which is not _ nor \s (whitespace character)
+ : one or more times
\s : whitespace character, i.e. space, no-break space, return, etc.
(?: . . . ) : non-captured group
* : zero or more times
\1 : back reference to the 1st captured group

boblines wrote:
Is this language easily learned, what is it called, and where to I go to learn about it?
It is called Regular Expression. There are several dialects. The one used in NW is almost the same as Perl’s. Many on-line tutorials are available. Personally I find this one nice.
but perhaps it is overkilling. You don’t need to learn all lessons. With the first four or five mastered to some degree, you would be able to facilitate many daily tasks, I think.

With Nisus Writer Express/Pro, it is rather easy to learn the Regular Expression. Its Find All works as Find and Select All. This means that you can test your expression on many sample texts at once and see all results with a single glance. That is a great advantage for learners of the Regular Expression.

Edit: The find expression above (step 3) is somewhat complicated. This is so because I don’t know your documents exactly. The following is expected to work as well.
. : any character except return (LF)
+? : shortest one or more times

Author:  boblines [ 2010-09-22 15:18:48 ]
Post subject:  Re: Find and replace

Thanks Kino et al : your help has made switching to Nisus an even more enjoyable experience.


Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group