Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > regex: multiline

Reply
Thread Tools

regex: multiline

 
 
Ahmet Kilic
Guest
Posts: n/a
 
      09-10-2009
Hello,

I am trying regex samples on this page
http://gnosis.cx/publish/programming...pressions.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/.

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      09-10-2009
Ahmet Kilic wrote:
> Hello,
>
> I am trying regex samples on this page
> http://gnosis.cx/publish/programming...pressions.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/.

 
Reply With Quote
 
 
 
 
Ahmet Kilic
Guest
Posts: n/a
 
      09-10-2009
thank you very much.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Ahmet Kilic
Guest
Posts: n/a
 
      09-10-2009
Robert Klemme wrote:
> 2009/9/10 7stud -- <>:
>>> 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 WORD) :String;
var
Buf: array[0..511] of Char;
MsgCnt WORD;
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?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      09-10-2009
2009/9/10 Ahmet Kilic <>:
> Robert Klemme wrote:
>> 2009/9/10 7stud -- <>:
>>>> 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 WORD) :String;
> var
> =A0Buf: array[0..511] of Char;
> =A0MsgCnt WORD;
> 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/

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      09-11-2009
Robert Klemme wrote:
> 2009/9/10 7stud -- <>:
>>> 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


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

 
Reply With Quote
 
Ahmet Kilic
Guest
Posts: n/a
 
      09-11-2009
Robert Klemme wrote:
> 2009/9/10 Ahmet Kilic <>:
>>>> 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.

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to define a variable to hold a multiline text input in perl from html multiline textbox dale zhang Perl Misc 8 11-30-2004 06:53 AM
adobe multiline substitution Justin Perl 0 12-08-2003 08:28 PM
Regular Expression for multiline validation Mark ASP .Net 3 08-05-2003 02:54 AM
Different fonts in multiline text box... Michael Tanner ASP .Net 1 07-25-2003 05:55 PM
how to limit user input into the multiline text box???? cw ASP .Net 1 06-27-2003 11:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57