![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Regular expression help in perl | sdubey | Shell Programming and Scripting | 3 | 05-22-2008 06:41 PM |
| S-037: Perl-Compatible Regular Expression (PCRE) Vulnerabilities | iBot | Security Advisories (RSS) | 0 | 12-24-2007 09:40 AM |
| Regular expression matching a new line | drheams | Shell Programming and Scripting | 1 | 12-13-2005 12:40 AM |
| Perl Regular Expression - Whitelist | mh53j_fe | Shell Programming and Scripting | 3 | 11-17-2005 08:31 PM |
| Perl Regular Expression - Whitlist | mh53j_fe | Shell Programming and Scripting | 3 | 11-01-2005 09:47 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Regular Expression matching in PERL
I am trying to read a file and capture particular lines into different strings:
Code:
LENGTH: Some Content here TEXT: Some Content Here COMMENT: Some Content Here |
|
||||
|
Thanks. Well, I have done that in my php version of the same code but heard that perl is really strong when it comes to regex so wanted to try out something new. Actually the problem is something like this:
Code:
LENGTH: ...................................................... .................................................................. .................................................................. ................................................................... .................................................................. SUBJECT: ....................................................... COMMENT: ..................................................... .................................................................... |
|
|||||
|
check if this works for you:
Code:
{
local $/; # reset the input record separator
$all_lines = <INPUT_FILE>; # Slurp the whole file in a string
}
while ($all_lines =~ m/LENGTH:(.*?)(SUBJECT|COMMENT)/g) {
push (@length_array, $1);
}
while ($all_lines =~ m/SUBJECT:(.*?)(LENGTH|COMMENT)/g) {
push (@subject_array, $1);
}
while ($all_lines =~ m/COMMENT:(.*?)(LENGTH|SUBJECT)/g) {
push (@comment_array, $1);
}
|
|
||||
|
Actually I was doing something on a similar lines:
Code:
$capture[0] = "LENGTH:";
$capture[1] = "COMMENT:";
$capture[2] = "BODY:";
$capture[3] = "AVATAR:";
$capture[4] = "POST:";
$capture[5] = "SUBJECT:";
$capture[6] = "DATE:";
$capture[7] = "";
open(DATA, "filename.txt");
$line = <DATA>;
if($line =~ /$capture[0](.*?)$capture[1]/sgm) {
$solution[0] = $1;
}
if($line =~ /$capture[1](.*?)$capture[2]/sgm) {
$solution[1] = $1;
}
if($line =~ /$capture[2](.*?)$capture[3]/sgm) {
$solution[2] = $1;
}
if($line =~ /$capture[3](.*?)$capture[4]/sgm) {
$solution[3] = $1;
}
if($line =~ /$capture[4](.*?)$capture[5]/sgm) {
$solution[4] = $1;
}
if($line =~ /$capture[5](.*?)$capture[6]/sgm) {
$solution[5] = $1;
}
if($line =~ /$capture[6](.*?)$capture[7]/sgm) {
$solution[6] = $1;
}
print trim($solution[0])."\n";
print trim($solution[1])."\n";
print trim($solution[2])."\n";
print trim($solution[3])."\n";
print trim($solution[4])."\n";
print trim($solution[5])."\n";
print trim($solution[6])."\n";
EDIT: Well, works like a charm if I embed your logic into mine I just changed the if into a while... Great! Thanks a lot for your help... |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| awk, awk trim, perl, perl regex, perl slurp, regex, trim, trim awk |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|