Find Replace - With wild cards
Hi
I have a small issue.
I'd like to find occurances of square brackets [], or this brackets (), in a word document and check if number or alphabet is immediately preceeding or suffixing it without space and if so insert one space.
Eg. my house[VBF]is in china
This I want to change it to my house [VBF] is in china
Eg. my house(VBF)is in china
This I want to change it to my house (VBF) is in china
yes I used the wild card option in more..
I believe it goes something like this in Find section: [A-Za-z0-9]\(
I get struck up in the Replace section:???????
Thanks in advance...:afrog:
patience is not the ability to wait but the ability to keep a good attitude while waiting :wave::wave:
Re: Find Replace - With wild cards
several weeks...still no solution leave alone response :eek:
unbelievable :rolleyes:
Also I discovered its something to do with Regular expressions...:wave:
Re: Find Replace - With wild cards
i remember trying to do this some time ago, but i think my internet crashed or some such
i believe you would have to use a VBA procedure to do this, only way i figured was to use replace to put space next to each bracket (before or after), then replace double spaces with single space, either in all instance, or when next to each bracket
Re: Find Replace - With wild cards
Quote:
use replace to put space next to each bracket (before or after),
That exactly is the issue...
Quote:
check if number or alphabet is immediately preceeding or suffixing it without space and if so insert one space.
how to add the space without deleting the existing character (num/alphabet) ???
Re: Find Replace - With wild cards
Code:
To use less brain power, an easy way is to replace
"[" with " ["
"]" with "] "
"(" with " ("
")" with ") "
and then at the end
" " with " "
Re: Find Replace - With wild cards
anhn that was good response....but...
Quote:
check if number or alphabet is immediately preceeding or suffixing it is without space and if so insert one space
Re: Find Replace - With wild cards
Try it, it will work: Not just number or alphabet but if not-a-space.
Re: Find Replace - With wild cards
I'll try..sure...also let me know how to add space before numbers...
Re: Find Replace - With wild cards
if you really need to only add the space where character is number or letter (so not punctuation or other brackets, or like!@#$$%^&* etc), the you would need to use VBA to find each bracket, check the character before (or after), then add space as appropriate, then find next bracket and repeat process, you would need to use instr to do this, otherwise loop through every character, to determine if it is a bracket
you can not do what you originally specified using words find /replace method
Re: Find Replace - With wild cards
The current issue is slightly away from the main topic...but related...so please bear with me...
Quote:
only add the space where character is number
Yes, but only if it already has no space.
Quote:
so not punctuation
They are taken care of..
Take a look at this...
Quote:
the age of the building is200years.
so here we need to search out the whole number sequence in this eg. its 200 and add space before & after.
Other combinations would be
Quote:
the age of the building is 200years.
Here we add space only after 200
Quote:
the age of the building is200 years.
Here we add space only before 200
Re: Find Replace - With wild cards
Quote:
The current issue is slightly away from the main topic...but related..
you can test this, it should do for both
vb Code:
Dim char As Range, chars As String, alpha As String, notnums as string
alpha = "ABCDEFGHIJKLNOPQRSTUVWXYZ0123456789"
notnums "ABCDEFGHIJKLNOPQRSTUVWXYZ"
For Each char In ThisDocument.Content.Characters
Select Case char
Case "(", "["
If InStr(1, alpha, ThisDocument.Characters(char.Start), vbTextCompare) > 0 Then char.InsertBefore " "
Case ")", "]"
If InStr(1, alpha, ThisDocument.Characters(char.End + 1), vbTextCompare) > 0 Then char.InsertAfter " "
Case 0 To 9
If InStr(1, notnums, ThisDocument.Characters(char.Start), vbTextCompare) > 0 Then char.InsertBefore " "
If InStr(1, notnums, ThisDocument.Characters(char.End + 1), vbTextCompare) > 0 Then char.InsertAfter " "
End Select
Next
minimal testing done, as checks every character in the document, it may be too slow, but it does test for all brackets and numbers, you can add any other required characters to strings for them to be included for spaces to be inserted
Re: Find Replace - With wild cards
I added your code in a sub just for the 0-9 case (at present) and ran it..nothing happened. Then I put the break point at "Case 0-9" and found that the loop does not enter the line next to it....
Code:
Dim char As Range, chars As String, alpha As String, notNums As String
alpha = "ABCDEFGHIJKLNOPQRSTUVWXYZ0123456789"
notNums = "ABCDEFGHIJKLNOPQRSTUVWXYZ"
For Each char In ThisDocument.Content.Characters
Select Case char
Case 0 To 9
If InStr(1, notNums, ThisDocument.Characters(char.Start), vbTextCompare > 0) Then char.InsertBefore " "
If InStr(1, notNums, ThisDocument.Characters(char.End + 1), vbTextCompare > 0) Then char.InsertAfter " "
End Select
Next
BTW
Code:
notNums = "ABCDEFGHIJKLNOPQRSTUVWXYZ"
did not have "=" so I added it...
Re: Find Replace - With wild cards
Quote:
did not have "=" so I added it...
typo
Quote:
Then I put the break point at "Case 0-9" and found that the loop does not enter the line next to it....
works for me
Re: Find Replace - With wild cards
My logic in post#5 is:
1. Always add a space before "[" and "(" regardless they are preceeded by a space or not.
2. Always add a space after "]" and ")" regardless they are followed by a space or not.
3. In both cases, if a space (before or after) already existed, it will become double spaces; so just replace all double spaces with single space.
Easy!
Re: Find Replace - With wild cards
Quote:
My logic in post#5 is:
basically what i put in post #3, which did not completely fill the original request and did not cover the additional requirement in posts #8 /10
1 Attachment(s)
Re: Find Replace - With wild cards
Dear Anhn & west,
I did understand what you were saying and already implemented it in the same order as suggested by you....Thanks for your expert guidance.
Ive included a sample file for your quick reference.
In each word block (I believe its called simply word in Office VBA) one must check for series of alpahbets or numbers. If there exist no space after the end of series then we must add a space.
eg. This document has200pages.
Here has200pages is a block. First we identify the start series, in the present case 'has'
this is followed by numbers ie. '200',
there is no intervening space so we add one space
this is followed by numbers ie. 'pages',
here again there is no intervening space so we add one space
Imp: Existing data must not be deleted
Re: Find Replace - With wild cards
guys any more inputs.........
Re: Find Replace - With wild cards
as my testing for the code in post #11 appeared to work correctly i do not understand why it did not work for you