Find And Replace 3 Characters

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have inherited an old word basic program. Part of the code in the program searches for text in a word document that has an @ sign and two letters after it and replaces it with blanks, i.e. @AB gets replaced with "". Here is the code

WordBasic.EditReplace Find:="\@??", Replace:="", Direction:=0, PatternMatch:=1, Format:=0, Wrap:=1, ReplaceAll:=

What I want to do is increase the number of characters from 2 after the @ sign to 3, i.e. @ABC gets replaced with "" or @AB gets replaced with "". When I try and simply add another ? (\@???), it doesn't work. Has anyone done this before? What am I doing wrong

Thanks

Mar
 
From the Edit menu, select Replace and then click on the More button and
check the Wildcards box. Then into the "Find what:" control, enter

\@[A-Z]{2,3}>

Then click on Replace All. That will delete @AB and @abc, but leave @abcd
with

If you want to delete all instances of @followedbytwoormoreuppercaseletters,
then use

\@[A-Z]{2,}

See the article “Finding and replacing characters using wildcards†at for an
explanation of how this works:

http://word.mvps.org/FAQs/General/UsingWildcards.htm


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
Mark Gould said:
I have inherited an old word basic program. Part of the code in the
program searches for text in a word document that has an @ sign and two
letters after it and replaces it with blanks, i.e. @AB gets replaced with
"". Here is the code:
WordBasic.EditReplace Find:="\@??", Replace:="", Direction:=0,
PatternMatch:=1, Format:=0, Wrap:=1, ReplaceAll:=1
What I want to do is increase the number of characters from 2 after the @
sign to 3, i.e. @abc gets replaced with "" or @AB gets replaced with "".
When I try and simply add another ? (\@???), it doesn't work. Has anyone
done this before? What am I doing wrong?
 
Mark said:
How would I use your solution in my VBA code:

WordBasic.EditReplace Find:="\@??", Replace:="", Direction:=0,
PatternMatch:=1, Format:=0, Wrap:=1, ReplaceAll:=1



Hi Mark,

Simply insert the expression(s) that Doug gave in Find:="..."

To delete @ followed by two or three upper-case letters, followed by the
end of the "word", you would just insert the expression that Doug
suggested:
WordBasic.EditReplace Find:="\@[A-Z]{2,3}>", Replace:="", Direction:=0,
PatternMatch:=1, Format:=0, Wrap:=1, ReplaceAll:=1

Since you want to sometimes delete @ and the folllowing two characters, and
other times @ and the following three characters, you have to give some
further condition,

-- either on all characters that follow the "@"...
say, only upper case letters [A-Z] as in Doug's example, or only
numbers and lower-case letters [0-9a-z], or any characters but spaces/para
marks [!^32^13]

-- or on some character(s) in the text that is to be deleted...
say "\@[!^13]{1,2}:" for "@" followed by one or two arbitrary
characters, followed by ":",
I often use [!^13] rather than ? ... especially if ? doesn't work as
expected ;-)

-- or on the stuff that follows...
such as the end-of-word-anchor ">" in Dougs expression, or requesting
some white space or punctuation character explicitly, say
WordBasic.EditReplace Find:="\@[!^13]{2,3}([ .^13])", Replace:="\1",
....
for @ followed by any two or three characters, followed by a space,
dot, or para mark (which gets re-inserted).

Regards,
Klaus
 
That seems to work, however I have one user that has the following

@ABC@012@XY@45@[email protected]

When I use your solution, I get unpredictable results. I would like to get jus

rhb.123

Is there a way for me to do this

Thanks

Mar


----- Klaus Linke wrote: ----

Mark Gould wrote
How would I use your solution in my VBA code
PatternMatch:=1, Format:=0, Wrap:=1, ReplaceAll:=



Hi Mark

Simply insert the expression(s) that Doug gave in Find:="...

To delete @ followed by two or three upper-case letters, followed by th
end of the "word", you would just insert the expression that Dou
suggested
WordBasic.EditReplace Find:="\@[A-Z]{2,3}>", Replace:="", Direction:=0
PatternMatch:=1, Format:=0, Wrap:=1, ReplaceAll:=

Since you want to sometimes delete @ and the folllowing two characters, an
other times @ and the following three characters, you have to give som
further condition

-- either on all characters that follow the "@"..
say, only upper case letters [A-Z] as in Doug's example, or onl
numbers and lower-case letters [0-9a-z], or any characters but spaces/par
marks [!^32^13

-- or on some character(s) in the text that is to be deleted..
say "\@[!^13]{1,2}:" for "@" followed by one or two arbitrar
characters, followed by ":"
I often use [!^13] rather than ? ... especially if ? doesn't work a
expected ;-

-- or on the stuff that follows..
such as the end-of-word-anchor ">" in Dougs expression, or requestin
some white space or punctuation character explicitly, sa
WordBasic.EditReplace Find:="\@[!^13]{2,3}([ .^13])", Replace:="\1"
...
for @ followed by any two or three characters, followed by a space
dot, or para mark (which gets re-inserted)

Regards
Klau
 
@ABC@012@XY@45@[email protected]
[...] I would like to get just

rhb.1234




Hi Mark,

The name of the game is "pattern matching"... And it's a bit hard to make
out a pattern from a sample of one.

So let's assume that you want to get rid of @ followed by 2 or three
characters if those characters are upper case or numbers.
There doesn't have to be a word boundary (space, punctuation, ...) after
the stuff you want to delete.

If you start from Doug's expression:
\@[A-Z]{2;3}>

You need to match numbers, too:
\@[A-Z0-9]{2;3}>
and remove the condition of the ">" end-of-word anchor:
\@[A-Z0-9]{2;3}

If your sample line wasn't really typical, you may have to change some
more.
But for the sample, everything but rhb.1234 should be deleted.

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top