-
November 4th, 2010, 07:17 PM
#1
Member
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.
-
November 7th, 2010, 01:03 AM
#2
Junior Member
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
[SIGPIC] http://www.opensuse.org/en/[/SIGPIC]
-
November 8th, 2010, 05:47 PM
#3
Member
A regex should be pretty universal, I am using Expresso as the designer tool.
-
November 9th, 2010, 06:50 PM
#4
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.
-
November 10th, 2010, 07:21 PM
#5
Member
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
-
Forum Rules
|
|