Number of characters found by a search expression

General questions about using TextPad

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

Post Reply
Aneesh
Posts: 6
Joined: Thu Nov 23, 2006 5:28 pm

Number of characters found by a search expression

Post by Aneesh »

Hi,

I am using the regex "([0-9]+)" to find all numbers enclosed within quotes. The requirement is to replace the numbers by an equivalent number of spaces ie
"12" -> 4 spaces
"1" -> 3 spaces
"1234" -> 6 spaces

Can this be done?

Thanks in advance,
Aneesh.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Did you mean:
"12" -> 2 spaces
"1" -> 1 spaces
"1234" -> 4 spaces
?

You can simply change each digit to a space:
Find what: [0-9]
Replace with: [one space]

[X] Regular expression
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

"12" -> 4 spaces
"1" -> 3 spaces
"1234" -> 6 spaces
It looks like the replacement is the number of characters found + 2 ?
So, "123" -> 5 spaces?, 1234567 -> 9 spaces?
Hope this was helpful.............good luck,
Bob
Aneesh
Posts: 6
Joined: Thu Nov 23, 2006 5:28 pm

Post by Aneesh »

Hi,

The 2 extra spaces are for the quoes enclosing the numbers. I need to replace only those numbers that occur within quotes. The regex i am using is "([0-9]+)" (the quotes are part of the regex). The number of spaces will be 2 more than the number of digits within quotes.

"12" -> 4 spaces (the 2 digits + 2 quotes)

Thanks,
Aneesh.
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

May have to do recursive Search/Replace. But once defined, could be made into a macro.
Example is using "-" to represent a visual representation of invisible spaces
------------------------------------------------------

Search for: "[0-9]{1}"
Replace with: "---"

Search for: "[0-9]{2}"
Replace with: "----"

Search for: "[0-9]{3}"
Replace with: "-----"
...
...
...

Search for: "[0-9]{10}"Replace with: "------------"
Hope this was helpful.............good luck,
Bob
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

You can do it in one go in WildEdit (replace each - with a space):
Find what: "(?:([0-9])|([0-9]){2}|([0-9]){3}|([0-9]){4})"
Replace with: ?1(---):?2(----):?3(-----):?4(------)

[X] Regular expression
[X] Replacement format
This will find ([0-9]) or ([0-9]){2} or ... within quotes. Extend as required.
Note the use of the construct (?:...) instead of (...) at the outer level. This groups the items within it, but doesn't mark them for use in the replacement expression; so they don't interfere with the subexpression numbering that we are about to use.

The replacement expression uses conditional expressions. If the first subexpression within (...) matched, use the parenthesised replacement following ?1; if the second subexpression matched, use the one following ?2; ...
Look for Conditional expressions in the help under Reference | Replacement Format Syntax.
Post Reply