Page 1 of 1

Number of characters found by a search expression

Posted: Fri Dec 08, 2006 12:34 pm
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.

Posted: Fri Dec 08, 2006 1:14 pm
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

Posted: Fri Dec 08, 2006 4:56 pm
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?

Posted: Fri Dec 08, 2006 5:19 pm
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.

Posted: Fri Dec 08, 2006 9:12 pm
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: "------------"

Posted: Fri Dec 08, 2006 11:11 pm
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.