Delete a paragraph up to a specific character

T

toth.usenet

hi,

i am trying to find a way to delete all text/characters in a paragraph
up to (and including) the last occurence of a backslash within that
paragraph, throughout the document. using the GNU regexp i would
select something like "^.*\\" and delete it.

so from a paragraph like

Subject\heading\section1\part2\topic3

only the following should remain

topic3

i tried to get this using search/replace but i failed with the unknown
number of characters which are to be replaced and the beginning of the
paragraph. please also note that some paragraphs have automatic
numbering but i assume that this should not make any difference as the
numbers should stay as they are.

any help on this is appreciated.

cheers,
thomas
 
T

Tony Jollans

This, unfortunately, appears to be more complex than it ought to be.

The only way to identify the start of a paragraph is by the paragraph mark
before it so your (wildcard) search string must begin with ^13 and will
never find anything in the first paragraph. After that, you should,
*in*theory* be able to look for any number of [not paragraph mark] followed
by [backslash] followed by any number of [not backslash] followed by
(another) paragraph mark - an expression of ^13[!^13]@\\([!\\]@^13) - where
the parenthetical expression is the bit you want to keep - and replace it
with ^13\1. Because the find includes the paragraph mark at the end of the
previous paragraph this may fail to find consecutive matching paragraphs but
a second pass should cure that problem.

There is a separate problem replacing with ^13 characters. Although not
immediately obvious in the UI, the replacement ^13 characters are just
that - characters; they are not paragraph marks. You should be able to get
round this by replacing with the found item instead of a new character as
per the expressions shown at the end.

However, my experiments show that this finds complete paragraphs that do not
contain any backslashes and I don't know why, nor can I work out a way to
stop it happening. They do not get changed (well, they get changed to
themselves) but the mere fact of them being found stops paragraphs following
them being correctly identified.

What does seem to work is a multi-step process.

1. Change all paragraph marks - use a search string of (^13) - note the
parentheses - to paragraph marks followed by some character not otherwise in
the document, say \1^23. Then add a ^23 character at the start of the
document.

2. Replace all ^23[!^13]@\\([!\\]@^13) with \1

3. Replace all ^23 with [nothing]
 
H

Helmut Weber

Hi Thomas,

sometimes it is easier to avoid a wildcard search.

Have a look at that one:

Sub Test567()
Dim oPrg As Paragraph
Dim rTmp As Range
For Each oPrg In ActiveDocument.Paragraphs
Set rTmp = oPrg.Range
With rTmp.Find
.Text = "\"
.Forward = False '!
While .Execute
rTmp.Start = oPrg.Range.Start
rTmp.Select ' for testing
rTmp.Text = ""
Wend
End With
Next
End Sub
 
J

Julian

Finding the last occurrence of something working forwards is a pain... so I
prefer to find the 1st occurrence of the same working backwards.

So, no code but I suspect this might be even easier: set a range =
para.range, collapse range to end, then MoveStartUntil backslash with
direction backwards...

Then set a range to begin with the paragraph and and set the range.end to be
the location just found and clear it
 
D

David Sisson

Another approach...

Sub DeleteUntilSlash()

Dim TheOffset As Integer
Dim MyRng As Range

For A = 1 To ActiveDocument.Paragraphs.Count

Set MyRng = ActiveDocument.Paragraphs(A).Range

MyRng.MoveEnd , -1
TheOffset = InStr(MyRng, "\")
If TheOffset > 1 Then
MyRng = StrReverse(MyRng)
MyRng = Left(MyRng, InStr(MyRng, "\") - 1)
MyRng = StrReverse(MyRng)
End If

Next

End Sub
 
J

Julian

OK... <rolls up sleeves>

Sub delToLast()
Dim aPara as Paragraph
Dim subStr() as String

for each aPara in ActiveDocument.Paragraphs
if aPara.range.text <>"" then
subStr = split(aPara.range.text, "\")
if Ubound(subStr) > 0 then
aPara.range.text = subStr(Ubound(subStr))
endif
endif
next
end sub
 

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

Top