This is my example;
…
*****************************************************************************)
function TfrmExecMain.SetPrivilege(PrivilegeName: String;
Enable: Boolean): Boolean;
var
tpPrev,
tp : TTokenPrivileges;
token : THandle;
dwRetLen : DWord;
begin
…
I want to print some parts of this.
for example;
if function=~/\b(^function)\s+(\w+)/
print this part===> : Boolean;
end
if line.match(/var/) and line.match(/begin/)
print this part ==> : TTokenPrivileges;
print this part ==> : THandle;
and print this ==> : DWord;
end
function TfrmExecMain.SetPrivilege(PrivilegeName: String;
for example;
ENDOFSTRING
md = str.match(/function.?):\s+(.?;)/m)
if md
 puts md[1]
end
–output:–
Boolean;
Note that you do not need the /m flag for this to match. This works
the same without it. The only difference that /m makes is whether
newlines are matched by .:
This is my example;
…
*****************************************************************************)
function TfrmExecMain.SetPrivilege(PrivilegeName: String;
Enable: Boolean): Boolean;
var
tpPrev,
tp : TTokenPrivileges;
token : THandle;
dwRetLen : DWord;
begin
…
I want to print some parts of this.
for example;
if function=~/\b(^function)\s+(\w+)/
print this part===> : Boolean;
end
if line.match(/var/) and line.match(/begin/)
print this part ==> : TTokenPrivileges;
print this part ==> : THandle;
and print this ==> : DWord;
end
How can i do it?
str = <<ENDOFSTRING
function TfrmExecMain.SetPrivilege(PrivilegeName: String;
Enable: Boolean): Boolean;
var
tpPrev,
tp : TTokenPrivileges;
token : THandle;
dwRetLen : DWord;
begin
ENDOFSTRING
md = str.match(/function.?):\s+(.?;)/m)
if md
puts md[1]
end
–output:–
Boolean;
results = []
should_parse = false
str.each_line do |line|
line = line.chomp
if line == “var”
should_parse = true
elsif line == “begin”
should_parse = false
end
if should_parse
line.scan(/\w+;$/) do |match|
results << match
end
end
end
for example;
tpPrev,
tp : TTokenPrivileges;
token : THandle;
dwRetLen : DWord;
begin
…
how can I do it with your code?
should I write new regexp for each function?
No, you simply need to replace the outer match with a call to
String#scan.
function TfrmExecMain.SetPrivilege(PrivilegeName: String;
for example;
ENDOFSTRING
md = str.match(/function.?):\s+(.?;)/m)
if md
 puts md[1]
end
–output:–
Boolean;
Note that you do not need the /m flag for this to match. This works
the same without it. The only difference that /m makes is whether
newlines are matched by .:
Thank you Robert,
Wooow! your code is seen perfect.
I want to ask one more question; my file contains many many functions,
vars and begins like this,
for example;
…
function TfrmExecMain.GetApiErrorMessage(ApiErrCode :DWORD) :String;
var
Buf: array[0…511] of Char;
MsgCnt :DWORD;
begin
…
function TfrmExecMain.SetPrivilege(PrivilegeName: String;
Enable: Boolean): Boolean;
var
tpPrev,
tp : TTokenPrivileges;
token : THandle;
dwRetLen : DWord;
begin
…
how can I do it with your code?
should I write new regexp for each function?
for example;
�tpPrev,
�tp � � � � : TTokenPrivileges;
�token � � �: THandle;
�dwRetLen � : DWord;
begin
…
how can I do it with your code?
should I write new regexp for each function?
No, you simply need to replace the outer match with a call to
String#scan.
Cheers
robert
If I write this part of code it is seen OK for me,
str.scan(/[^;]+;/) do |var|
if /\A([^:]+):([^:]+);\z/ =~ var
names = $1
type = $2.strip
names.split(/,/).each do |name|
printf "var: type: %-20s name: %-20s\n", type, name.strip
end
else
puts "error with var #{var.inspect}"
end
end
but
No, you simply need to replace the outer match with a call to
String#scan.
what is the “replace the outer match” meaning? please explain a bit
more.
7stud: I recognize it also. It is not working for me. I am using ruby
1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
thanks.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.