openoffice-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "F C. Costero" <fjcc.apa...@gmail.com>
Subject Re: Find and Replace Macro with regex captured groups in replace text
Date Wed, 01 Jan 2014 23:16:27 GMT
The problem is the line
oFind.setString(FandR.ReplaceString)

The setString() method takes a string input and doesn't know anything about
the search done with oDoc.FindNext() or regular expressions. You can
perform a global replace that implements your regular expression with
oDoc.replaceAll(FandR)
I tried this for the string budh and got

budh 1P = b[guṇa udh]->b[a+udh]->bodh + a->bodha
which is not exactly what you expected.
I don't at the moment see how to do a local replace but I'm no expert in
such things.
Best regards,
Francis

On Tue, Dec 31, 2013 at 1:31 PM, Harry Spier <vasishtha.spier@gmail.com>wrote:

> Dear list members,
>
> I need to write a macro that tries to match a series of regexs against some
> text I've selected and then replace it with a replacement string that
> contains captured groups.
>
> I've tried the following (borrowed and modified from some on-line code) but
> it doesn't recognise $0,$1,$2,$3 as captured groups and just prints them
> out as literals.
> This is what I've tried:
> ---------------------------
> Sub Stem1P()
> Dim oDoc,oText,oVC,oStart,oEnd,oFind,FandR
> oDoc = ThisComponent : oText = oDoc.Text
> oVC = oDoc.CurrentController.getViewCursor
>
>
> aFind = Array(
> "([^aāiīuūṛṝḷḹeo[:space:]])(a)([^aāiīuūṛṝḷḹeo]h{0,1})[:space:]{0,1}",_
> "([^aāiīuūṛṝḷḹeo[:space:]]*)(i)([^aāiīuūṛṝḷḹeo]h{0,1})[:space:]{0,1}",_
> "([^aāiīuūṛṝḷḹeo[:space:]])(u)([^aāiīuūṛṝḷḹeo]h{0,1})[:space:]{0,1}",_
> "([^aāiīuūṛṝḷḹeo[:space:]]*)(ṛ)([^aāiīuūṛṝḷḹeo]h{0,1})[:space:]{0,1}",_
> "([^aāiīuūṛṝḷḹeo[:space:]]*)(ḷ)([^aāiīuūṛṝḷḹeo]h{0,1})[:space:]{0,1}")
>
>
>
>
> aReplace = Array ( "$0 1P = $1[guṇa $2$3]->$1[$2$3]->$1a$3 + a->$1a$3a",_
> "$0 1P = $1[guṇa $2$3]->$1[a+$2$3]->$1e$3 + a->$1e$3a",_
> "$0 1P = $1[guṇa $2$3]->$1[a+$2$3]->$1o$3 + a->$1o$3a",_
> "$0 1P = $1[guṇa $2$3]->$1[a+$2$3]->$1ar$3 + a->$1ar$3a",_
> "$0 1P = $1[guṇa $2$3]->$1[a+$2$3]->$1al$3 + a->$1al$3a")
>
>
> aRayCount = 0
> While aRayCount <= uBound(aFind)
> oStart = oText.createTextCursorByRange(oVC.Start)
> oEnd = oText.createTextCursorByRange(oVC.End)
> FandR = oDoc.createReplaceDescriptor
> With FandR
> .SearchString = aFind(aRayCount)
> .ReplaceString = aReplace(aRayCount)
> .searchCaseSensitive = true
> .SearchWords = false
> .SearchRegularExpression = true
> End With
> Do
> oFind = oDoc.FindNext(oStart.End,FandR)
> If isNull(oFind) then Exit Do
> If oText.compareRegionEnds(oFind,oEnd) < 0 then Exit Do
> oFind.setString(FandR.ReplaceString)
> oFind = oDoc.FindNext(oFind.End,FandR)
> Loop
>
> aRayCount = aRayCount + 1
> Wend
> End Sub
> --------------------
> When I select the text   budh    and then run the macro it doesn't
> recognize $0,$1,$2,$3 as captured groups but prints:
>
> $0 1P = $1[guṇa $2$3]->$1[a+$2$3]->$1o$3 + a->$1o$3a
> instead of what I want which is :
>
> budh 1P = b[guṇa udh]->b[a+udh]->bedh + a -> budha
>
> Thanks,
> Harry Spier
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message