locating non-zero values

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
obfusc88
Posts: 8
Joined: Mon Dec 18, 2006 8:50 pm

locating non-zero values

Post by obfusc88 »

A question for ben_josephs or Bob Hansen:

This is the test string (with spaces):
0 0 0 0 0 0 0 0 0 0 0 0%lf% 0 0 0 0 0 0 0 0 0 0 0 0%lf% 0 0 0 0 0 0 0 0 34 34 34 0 0 0%lf% 0 0 0 0 0 0 0 0 0 0 0 0%lf%

The task is to locate the non-zero values. I am using "_" for space char in Search/Replace strings.

This does work, ends up with 0 xxx xxx xxx 0 where I expect it.
Search for: [^0_][1-9]_
Replace with: xxx_

This does not work, ends up with 0 xxx 34 xxx 0 in the same area
Search for: _[^0 ][1-9]_
Replace with: _xxx_

If I single step througn current document, this last expression works OK, but if I do a mass update through Selected Text, it fails as shown above. The only difference is the leading space chars.

Can you please provide an explanation about the non-working syntax?

I still have some unknowns about the non-zero values, I do not know how many digits are valid, and I don't know if two or more digits would have leading zeros so that two digits seven is either "7" or "07"
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

_[^0 ][1-9]_ matches a number together with the space before it and the space after it.

When you Replace All, after each replacement the cursor is left at the end of the replacement text, that is, just past the space at the end of it. So the cursor is now immediately in front of the next number to be replaced -- the space in front of it has been consumed. The regex requires that leading space, so it cannot match the next number, and matches the one after it instead.

On the other hand, when you make replacements one at a time, after each replacement the position is left just after the first character of the replacement text. The leading space of the next number has not been consumed, and so the regex can match the next number.

Here's an alternative regex; it matches any sequence of digits that isn't all zeros:
\<[0-9]*[1-9][0-9]*\>
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

Here is a visual aid to supplement ben_josephs terrific explanation. Using ^ to show the cursor position after the first replacement, using "_" for spaces:

When you Replace All, after each replacement the cursor is left at the end of the replacement text, that is, just past the space at the end of it. So the cursor is now immediately in front of the next number to be replaced -- the space in front of it has been consumed. The regex requires that leading space, so it cannot match the next number, and matches the one after it instead.
0_0_xxx_^34_34_0_0
---------------------------------------------------------------

On the other hand, when you make replacements one at a time, after each replacement the position is left just after the first character of the replacement text. The leading space of the next number has not been consumed, and so the regex can match the next number.
0_0_^xxx_34_34_0_0
------------------------------------------------------------------

You can see this in the single replacements in Active Document. Use Find Next, Then Replace. Now click on the TextPad Title Bar so you do not change anything in the body of the document. Now press the Back Arrow to see where the cursor was positioned. You should now see the cursor to the left of the space char. Prior to the Back Arrow, it was after the space and before the xxx.
Hope this was helpful.............good luck,
Bob
Post Reply