regex: multiline

A

Ahmet Kilic

Hello,

I am trying regex samples on this page
http://gnosis.cx/publish/programming/regular_expressions.html.
My problem is, how can I grep multiline with regex?

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?
 
7

7stud --

Ahmet said:
Hello,

I am trying regex samples on this page
http://gnosis.cx/publish/programming/regular_expressions.html.
My problem is, how can I grep multiline with regex?

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

p results

--output:--
["TTokenPrivileges;", "THandle;", "DWord;"]
 
A

Ahmet Kilic

Robert said:
2009/9/10 7stud -- said:
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 .:

irb(main):001:0> s = "a\nb\n"
=> "a\nb\n"
irb(main):002:0> s[/.*/]
=> "a"
irb(main):003:0> s[/.*/m]
=> "a\nb\n"

It does not make any difference for \s:

irb(main):004:0> s[/\s+/]
=> "\n"
irb(main):005:0> s[/\s+/m]
=> "\n"

I'll attach an extended example.

Kind regards

robert

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?
 
R

Robert Klemme

2009/9/10 Ahmet Kilic said:
Robert said:
2009/9/10 7stud -- said:
function TfrmExecMain.SetPrivilege(PrivilegeName: String;
for example;

ENDOFSTRING

md =3D str.match(/function.*?\):\s+(.*?;)/m)
if md
=A0puts md[1]
end

--output:--
Boolean;

Note that you do not need the /m flag for this to match. =A0This works
the same without it. =A0The only difference that /m makes is whether
newlines are matched by .:

irb(main):001:0> s =3D "a\nb\n"
=3D> "a\nb\n"
irb(main):002:0> s[/.*/]
=3D> "a"
irb(main):003:0> s[/.*/m]
=3D> "a\nb\n"

It does not make any difference for \s:

irb(main):004:0> s[/\s+/]
=3D> "\n"
irb(main):005:0> s[/\s+/m]
=3D> "\n"

I'll attach an extended example.

Kind regards

robert

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
=A0Buf: array[0..511] of Char;
=A0MsgCnt :DWORD;
begin
...
function TfrmExecMain.SetPrivilege(PrivilegeName: String;
=A0Enable: Boolean): Boolean;
var
=A0tpPrev,
=A0tp =A0 =A0 =A0 =A0 : TTokenPrivileges;
=A0token =A0 =A0 =A0: THandle;
=A0dwRetLen =A0 : 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


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
7

7stud --

Robert said:
2009/9/10 7stud -- said:
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.

Note that you are wrong:

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)
p md
md = str.match(/function.*?\):\s+(.*?;)/)
p md

--output:--
#<MatchData:0x2529c>
nil
 
A

Ahmet Kilic

Robert said:
2009/9/10 Ahmet Kilic said:
end
irb(main):002:0> s[/.*/]
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.
 

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

Similar Threads

Get functions names with regex 5
RegEx 0
Syck dies on multiline regex 2
Chatbot 0
Tasks 1
multiline regexp and newlines 2
JavaScript Challenge: Validating Email Addresses 1
Can't solve problems! please Help 0

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top