regex: multiline

Discussion in 'Ruby' started by Ahmet Kilic, Sep 10, 2009.

  1. Ahmet Kilic

    Ahmet Kilic Guest

    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, Sep 10, 2009
    #1
    1. Advertising

  2. Ahmet Kilic

    7stud -- Guest

    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/.
    7stud --, Sep 10, 2009
    #2
    1. Advertising

  3. Ahmet Kilic

    Ahmet Kilic Guest

    Ahmet Kilic, Sep 10, 2009
    #3
  4. Ahmet Kilic

    Ahmet Kilic Guest

    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 :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?
    --
    Posted via http://www.ruby-forum.com/.
    Ahmet Kilic, Sep 10, 2009
    #4
  5. 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 :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/
    Robert Klemme, Sep 10, 2009
    #5
  6. Ahmet Kilic

    7stud -- Guest

    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/.
    7stud --, Sep 11, 2009
    #6
  7. Ahmet Kilic

    Ahmet Kilic Guest

    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/.
    Ahmet Kilic, Sep 11, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yatima

    Multiline regex help

    Yatima, Mar 3, 2005, in forum: Python
    Replies:
    13
    Views:
    550
    Kent Johnson
    Mar 4, 2005
  2. eggie5

    multiline regex expression

    eggie5, Jul 21, 2007, in forum: Java
    Replies:
    4
    Views:
    503
    Roedy Green
    Jul 22, 2007
  3. Gilles Ganault

    [2.5] Regex doesn't support MULTILINE?

    Gilles Ganault, Jul 22, 2007, in forum: Python
    Replies:
    9
    Views:
    306
    Gilles Ganault
    Jul 24, 2007
  4. Replies:
    3
    Views:
    734
    Reedick, Andrew
    Jul 1, 2008
  5. dale zhang
    Replies:
    8
    Views:
    409
    Tintin
    Nov 30, 2004
Loading...

Share This Page