Find and replace

Have a problem? A question? This is the place for answers from other Express users.
Post Reply
boblines
Posts: 5
Joined: 2010-08-08 02:23:46

Find and replace

Post by boblines »

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?
Thanks
Bob
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Find and replace

Post by martin »

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:

Code: Select all

\(.+?\)
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:

Code: Select all

(?<=\().+?(?=\))
boblines
Posts: 5
Joined: 2010-08-08 02:23:46

Re: Find and replace

Post by boblines »

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

Bob
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Find and replace

Post by martin »

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

Code: Select all

\(.+?\)
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:

Code: Select all

(?<=\().+?(?=\))
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.
boblines
Posts: 5
Joined: 2010-08-08 02:23:46

Re: Find and replace

Post by boblines »

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
Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Find and replace

Post by Kino »

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:

Code: Select all

_([^_\s]+(?:\s+[^_\s]+)*)_
4. In the Replace with field, type this:

Code: Select all

\1
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.
http://www.regular-expressions.info/tutorialcnt.html
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.

Code: Select all

_(.+?)_
. : any character except return (LF)
+? : shortest one or more times
boblines
Posts: 5
Joined: 2010-08-08 02:23:46

Re: Find and replace

Post by boblines »

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

Bob
Post Reply