Regular Expressions and how they hate me
Results 1 to 5 of 5

Thread: Regular Expressions and how they hate me

  1. #1
    Member
    Join Date
    Apr 2006
    Posts
    66

    Regular Expressions and how they hate me

    hello all, I am having trouble with some regular expressions.
    Here is a file format that I have:
    Code:
    SECTION         Test
    Comp                  1
    Comp                  2
    Frame                 1
    Frame                 2
    SECTION        Test2
    Comp                  1
    (..etc..)
    What I need is a regular expression that just pulls out each section, so the first match would be everything from SECTION to SECTION.

    I am terrible at regular expressions and I am not sure how to get this done. Any ideas?
    Thanks in advance.

  2. #2
    Junior Member mostafaxx's Avatar
    Join Date
    Jul 2010
    Location
    Egypt-damanhour
    Posts
    15
    what is the programing language you're coding in ??
    Decode the following to 8-Bit ASCII : 01001001 01110011 01101100 01100001 01101101 00100000 01101001 01110011 00100000 01110100 01101000 01100101 00100000 01110011 01101111 01101100 01110101 01110100 01101001 01101111 01101110

    <a rel=http://www.opensuse.org/en/" border="0" />

  3. #3
    Member
    Join Date
    Apr 2006
    Posts
    66
    A regex should be pretty universal, I am using Expresso as the designer tool.

  4. #4
    Junior Member
    Join Date
    Apr 2004
    Location
    United States
    Posts
    24
    Regex can actually be very different from one implementation to another. I wrote this up real quick using GNU sed. Which I believe has a syntax very similar to perl, your mileage may vary in other languages.

    the contents the test file is:
    Code:
    $~/temp> cat file 
    SECTION         Test
    Comp                  1
    Comp                  2
    Frame                 1
    Frame                 2
    SECTION        Test2
    Comp                  1
    Frame                 1
    Frame                 2
    And here is my round about way of doing it:
    Code:
    $~/temp> sed -n '/^SECTION\s\+Test/,/^SECTION\s\+Test/ s/\(.\+\s\+[0-9]\+\)/\1/p' file
    Comp                  1
    Comp                  2
    Frame                 1
    Frame                 2
    To grab section Test2:
    Code:
    $~/temp> sed -n '/^SECTION\s\+Test2/,/^SECTION\s\+Test/ s/\(.\+\s\+[0-9]\+\)/\1/p' file
    Comp                  1
    Frame                 1
    Frame                 2
    There are probably other better ways, but that's what I came up with off the top of my head.

    The gist of what I'm doing is this:

    Match from ^SECTION\s\+Test to ^SECTION\s\+Test and then do a substitution for the pattern \(.\+\s\+[0-9]\+\) which gets stored in address space \1 (perl would call that $1), then print what is in \1, that way we get just the contents withing the matching section. The sed '-n' option tells it not to print anything unless we explicitly tell it to.

  5. #5
    Member
    Join Date
    Apr 2006
    Posts
    66
    ah, great thanks ABS. I'll give that a shot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •