Posts: 125
Threads: 20
Joined: Jan 2021
Thank you again, your programming is very good.
Posts: 125
Threads: 20
Joined: Jan 2021
01-22-2021, 11:20 PM
(This post was last modified: 01-24-2021, 09:53 AM by macman.)
@Kevin
How to exclude the strings for the comment line? I just want to find and replace the double byte string of non comment lines
I thought about it for a long time and didn't solve it
Add the following code, the comment is removed, but there are the following problems
1. There are extra spaces or empty lines
2. There is no comment in the replacement result
_______________________
_s.replacerx("//.*" "")
e6=_s
Macro Macro3
Trigger A1
_s=
;class Program
;{
;,static void Main()
;,{
;,,//文件名 文字 中文
;,,Section section = new Section(document);
;,,document.Sections.Add(section);
;
;,,//This is the comment 中文
;,,Paragraph paragraph = new Paragraph(文件);
;,,section.Blocks.Add(paragraph);
;
;,,Run run = new Run(document, "文字");
;,,paragraph.Inlines.Add(run);
;
;,,document.Save("文件名.docx");
;,}
;}
str dd=
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 334 200 "Dialog" "4"
;3 Edit 0x54030080 0x200 8 160 95 13 ""
;4 Edit 0x54030080 0x200 120 160 95 13 ""
;5 Edit 0x54030080 0x200 232 160 95 13 ""
;6 Edit 0x54231044 0x200 8 8 318 146 ""
;1 Button 0x54030001 0x4 216 180 48 14 "OK"
;2 Button 0x54030000 0x4 272 180 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2040B01 "*" "" "" ""
str controls = "3 4 5 6"
str e3 e4 e5 e6
_s.replacerx("//.*" "")
e6=_s
ARRAY(str) a
str pattern="[\x80-\xff]+" ;;Match all double byte characters
if(findrx(_s pattern 0 4 a)<0) out "does not match"; ret
for int'i 0 a.len
,sel i
,,case 0
,,e3 = a[0 i]
,,case 1
,,e4 = a[0 i]
,,case 2
,,e5 = a[0 i]
if(!ShowDialog(dd 0 &controls)) ret
int ci
for i 0 a.len
,sel i
,,case 0
,,ci=_s.findreplace(a[0 i] F"{e3}" 4)
,,case 1
,,ci=_s.findreplace(a[0 i] F"{e4}" 4 "" ci+e3.len)
,,case 2
,,ci=_s.findreplace(a[0 i] F"{e5}" 4 "" ci+e4.len)
mes _s
Posts: 1,338
Threads: 61
Joined: Jul 2006
here you go.
This will display the the original string in the dialog but only do replacements on non commented lines.
_s=
;class Program
;{
;,static void Main()
;,{
;,,//文件名 文字 文件
;,,Section section = new Section(document);
;,,document.Sections.Add(section);
;
;,,//This is the comment 文件
;,,Paragraph paragraph = new Paragraph(文件);
;,,section.Blocks.Add(paragraph);
;
;,,Run run = new Run(document, "文字");
;,,paragraph.Inlines.Add(run);
;
;,,document.Save("文件名.docx");
;,}
;}
str s; int i j
ARRAY(int) linenumbers
ARRAY(str) a b
a=_s
for i 0 a.len
,if(findrx(a[i] "//.*")>=0)
,,linenumbers[]=i
for(i a.len-1 -1 -1)
,for j 0 linenumbers.len
,,if(i=linenumbers[j])
,,,a.remove(i)
s=a
s.trim
str dd=
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 334 200 "Dialog" "4"
;3 Edit 0x54030080 0x200 8 160 95 13 ""
;4 Edit 0x54030080 0x200 120 160 95 13 ""
;5 Edit 0x54030080 0x200 232 160 95 13 ""
;6 Edit 0x54231044 0x200 8 8 318 146 ""
;1 Button 0x54030001 0x4 216 180 48 14 "OK"
;2 Button 0x54030000 0x4 272 180 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2040B01 "*" "" "" ""
str controls = "3 4 5 6"
str e3 e4 e5 e6
e6=_s;; show original string in edit6
a.redim
str pattern="[\x80-\xff]+" ;;Match all double byte characters
if(findrx(s pattern 0 4 a)<0) out "does not match"; ret
for i 0 a.len
,sel i
,,case 0
,,e3 = a[0 i]
,,case 1
,,e4 = a[0 i]
,,case 2
,,e5 = a[0 i]
if(!ShowDialog(dd 0 &controls)) ret
int ci
for i 0 a.len
,sel i
,,case 0
,,ci=s.findreplace(a[0 i] F"{e3}" 4)
,,case 1
,,ci=s.findreplace(a[0 i] F"{e4}" 4 "" ci+e3.len)
,,case 2
,,ci=s.findreplace(a[0 i] F"{e5}" 4 "" ci+e4.len)
b=s
for i 0 b.len
,for j 0 linenumbers.len
,,if(i=linenumbers[j])
,,,str ss.getl(_s linenumbers[j])
,,,b.insert(linenumbers[j])
,,,b[linenumbers[j]]=ss
_s=b
_s.trim
mes _s
Posts: 125
Threads: 20
Joined: Jan 2021
01-23-2021, 07:36 AM
(This post was last modified: 01-24-2021, 09:52 AM by macman.)
@ Kevin Thanks a lot.
running very well,
You can always find solutions
@kevin
1.How to find and replace from the modification results of E6 editbox control?
2.How to Send the modified content of E6 edit box directly to the input point(Can be realized without adding a dialog process sub function?)
Posts: 125
Threads: 20
Joined: Jan 2021
01-24-2021, 01:01 AM
(This post was last modified: 01-24-2021, 01:09 AM by macman.)
@ Kevin
The following code achieves the goal, but there is a problem:
In the result, the comment is removed
How to add the following code and keep the comments?
_______________________________________________
for i 0 b.len
for j 0 linenumbers.len
if(i=linenumbers[j])
str ss.getl(_s linenumbers[j])
b.insert(linenumbers[j])
b[linenumbers[j]]=ss
Macro Macro3
_s=
;class Program
;{
;,static void Main()
;,{
;,,//文件名 文字 中文
;,,Section section = new Section(document);
;,,document.Sections.Add(section);
;
;,,//This is the comment 中文
;,,Paragraph paragraph = new Paragraph(文件);
;,,section.Blocks.Add(paragraph);
;
;,,Run run = new Run(document, "文字");
;,,paragraph.Inlines.Add(run);
;
;,,document.Save("文件名.docx");
;,}
;}
str dd=
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 342 200 "Dialog" "4"
;3 Edit 0x54030080 0x200 8 160 95 13 ""
;4 Edit 0x54030080 0x200 120 160 95 13 ""
;5 Edit 0x54030080 0x200 232 160 95 13 ""
;6 Edit 0x54231044 0x200 8 8 326 146 ""
;7 Button 0x54032000 0x0 96 180 110 14 "send modified content of E6"
;1 Button 0x54030001 0x4 216 180 48 14 "OK"
;2 Button 0x54030000 0x4 272 180 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2040B01 "*" "" "" ""
str controls = "3 4 5 6"
str e3 e4 e5 e6
e6=_s
_s.replacerx("\s*//.*" "")
ARRAY(str) a
str pattern="[\x80-\xff]+" ;;Match all double byte characters
if(findrx(_s pattern 0 4 a)<0) out "does not match"; ret
for int'i 0 a.len
,sel i
,,case 0
,,e3 = a[0 i]
,,case 1
,,e4 = a[0 i]
,,case 2
,,e5 = a[0 i]
if(!ShowDialog(dd &sub.DlgProc &controls)) ret
_s=e6
_s.replacerx("\s*//.*" "")
int ci
for i 0 a.len
,sel i
,,case 0
,,ci=_s.findreplace(a[0 i] F"{e3}" 4)
,,case 1
,,ci=_s.findreplace(a[0 i] F"{e4}" 4 "" ci+e3.len)
,,case 2
,,ci=_s.findreplace(a[0 i] F"{e5}" 4 "" ci+e4.len)
mes _s
#sub DlgProc
function# hDlg message wParam lParam
sel message
,case WM_INITDIALOG
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,
,case 7;;Original
,str Ori.getwintext(id(6 hDlg))
,DT_Cancel(hDlg)
,mes Ori
ret 1
Posts: 1,338
Threads: 61
Joined: Jul 2006
try this
_s=
;class Program
;{
;,static void Main()
;,{
;,,//文件名 文字 中文
;,,Section section = new Section(document);
;,,document.Sections.Add(section);
;
;,,//This is the comment 中文
;,,Paragraph paragraph = new Paragraph(文件);
;,,section.Blocks.Add(paragraph);
;
;,,Run run = new Run(document, "文字");
;,,paragraph.Inlines.Add(run);
;
;,,document.Save("文件名.docx");
;,}
;}
str dd=
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 342 200 "Dialog" "4"
;3 Edit 0x54030080 0x200 8 160 95 13 ""
;4 Edit 0x54030080 0x200 120 160 95 13 ""
;5 Edit 0x54030080 0x200 232 160 95 13 ""
;6 Edit 0x54231044 0x200 8 8 326 146 ""
;7 Button 0x54032000 0x0 96 180 110 14 "send modified content of E6"
;1 Button 0x54030001 0x4 216 180 48 14 "OK"
;2 Button 0x54030000 0x4 272 180 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2040B01 "*" "" "" ""
str controls = "3 4 5 6"
str e3 e4 e5 e6
e6=_s
_s.replacerx("\s*//.*" "")
ARRAY(str) a
str pattern="[\x80-\xff]+" ;;Match all double byte characters
if(findrx(_s pattern 0 4 a)<0) out "does not match"; ret
for int'i 0 a.len
,sel i
,,case 0
,,e3 = a[0 i]
,,case 1
,,e4 = a[0 i]
,,case 2
,,e5 = a[0 i]
if(!ShowDialog(dd &sub.DlgProc &controls)) ret
ARRAY(int) linenumbers
ARRAY(str) b c
b=e6
for i 0 b.len
,if(findrx(b[i] "//.*")>=0)
,,linenumbers[]=i
for(i b.len-1 -1 -1)
,for int'j 0 linenumbers.len
,,if(i=linenumbers[j])
,,,b.remove(i)
str s=b
int ci
for i 0 a.len
,sel i
,,case 0
,,ci=s.findreplace(a[0 i] F"{e3}" 4)
,,case 1
,,ci=s.findreplace(a[0 i] F"{e4}" 4 "" ci+e3.len)
,,case 2
,,ci=s.findreplace(a[0 i] F"{e5}" 4 "" ci+e4.len)
c=s
for i 0 c.len
,for j 0 linenumbers.len
,,if(i=linenumbers[j])
,,,str ss.getl(e6 linenumbers[j])
,,,c.insert(linenumbers[j])
,,,c[linenumbers[j]]=ss
_s=c
out _s.rtrim
#sub DlgProc
function# hDlg message wParam lParam
sel message
,case WM_INITDIALOG
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
,case 7;;Original
,str Ori.getwintext(id(6 hDlg))
,DT_Cancel(hDlg)
,mes Ori
ret 1
Posts: 125
Threads: 20
Joined: Jan 2021
Thanks for your help @kevin
Now the code works well and It is easier to understand than the # 33 code
Posts: 1,338
Threads: 61
Joined: Jul 2006
I edited some code above to make it less and added comments
str original=
;class Program
;{
;,static void Main()
;,{
;,,//文件名 文字 中文
;,,Section section = new Section(document);
;,,document.Sections.Add(section);
;
;,,//This is the comment 中文
;,,Paragraph paragraph = new Paragraph(文件);
;,,section.Blocks.Add(paragraph);
;
;,,Run run = new Run(document, "文字");
;,,paragraph.Inlines.Add(run);
;
;,,document.Save("文件名.docx");
;,}
;}
str dd=
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 342 200 "Dialog" "4"
;3 Edit 0x54030080 0x200 8 160 95 13 ""
;4 Edit 0x54030080 0x200 120 160 95 13 ""
;5 Edit 0x54030080 0x200 232 160 95 13 ""
;6 Edit 0x54231044 0x200 8 8 326 146 ""
;7 Button 0x54032000 0x0 96 180 110 14 "send modified content of E6"
;1 Button 0x54030001 0x4 216 180 48 14 "OK"
;2 Button 0x54030000 0x4 272 180 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2040B01 "*" "" "" ""
str controls = "3 4 5 6"
str e3 e4 e5 e6
e6=original
original.replacerx("\s*//.*" "")
ARRAY(str) a
str pattern="[\x80-\xff]+" ;;Match all double byte characters
if(findrx(original pattern 0 4 a)<0) out "does not match"; ret
for int'i 0 a.len
,sel i
,,case 0
,,e3 = a[0 i]
,,case 1
,,e4 = a[0 i]
,,case 2
,,e5 = a[0 i]
if(!ShowDialog(dd &sub.DlgProc &controls)) ret
ARRAY(int) linenumbers
ARRAY(str) b c
b=e6
//remove comments in edited string and store line numbers into array linenumbers
for(i b.len-1 -1 -1)
,if(findrx(b[i] "//.*")>=0)
,,linenumbers[]=i
,,b.remove(i)
linenumbers.sort(8);;sort the array since line numbers are in decending order
str s=b
//do replacements of e3 e4 e5 in edited string
int ci j
for i 0 a.len
,sel i
,,case 0
,,ci=s.findreplace(a[0 i] F"{e3}" 4)
,,case 1
,,ci=s.findreplace(a[0 i] F"{e4}" 4 "" ci+e3.len)
,,case 2
,,ci=s.findreplace(a[0 i] F"{e5}" 4 "" ci+e4.len)
c=s
// add comments back to edited string
for i 0 c.len
,for j 0 linenumbers.len
,,if(i=linenumbers[j])
,,,str ss.getl(e6 linenumbers[j])
,,,c.insert(linenumbers[j])
,,,c[linenumbers[j]]=ss
_s=c
// remove any empty lines from beginning and end and show the result
out _s.trim("[13][10]")
#sub DlgProc
function# hDlg message wParam lParam
sel message
,case WM_INITDIALOG
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
,case 7;;modified text of e6
,str mod.getwintext(id(6 hDlg))
,DT_Cancel(hDlg)
,mes mod
ret 1
Posts: 125
Threads: 20
Joined: Jan 2021
@kevin
Thanks for sharing, the code works very well
|