Regex: multiline

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?

···

--
Posted via http://www.ruby-forum.com/.

Ahmet Kilic wrote:

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;"]

···

--
Posted via http://www.ruby-forum.com/\.

thank you very much.

···

--
Posted via http://www.ruby-forum.com/.

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

ml.rb (1.08 KB)

···

2009/9/10 7stud -- <bbxx789_05ss@yahoo.com>:

Ahmet Kilic wrote:

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;

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

Robert Klemme wrote:

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?

···

2009/9/10 7stud -- <bbxx789_05ss@yahoo.com>:

--
Posted via http://www.ruby-forum.com/\.

Robert Klemme wrote:

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

···

2009/9/10 7stud -- <bbxx789_05ss@yahoo.com>:

--
Posted via http://www.ruby-forum.com/\.

No, you simply need to replace the outer match with a call to String#scan.

Cheers

robert

···

2009/9/10 Ahmet Kilic <ahmedkilic@gmail.com>:

Robert Klemme wrote:

2009/9/10 7stud -- <bbxx789_05ss@yahoo.com>:

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?

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

Robert Klemme wrote:

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.

···

2009/9/10 Ahmet Kilic <ahmedkilic@gmail.com>:

--
Posted via http://www.ruby-forum.com/\.