how to parse string from [?

PERL

    Next

  • 1. (..) and (...) question
    According to Perl Cookbook, the three dots (...) should be "not inclusive" while the two dots (..) "inclusive" when used with line numbers. By "not inclusive", I would think that the code below will print only line 2, but it prints the same first three lines just like using (..). Can someone please explain? Thanks! #!perl use strict; use warnings; while ( <DATA> ){ chomp; if ( 1 ... 3 ){ print $_, "\n"; } } __DATA__ Line 1 Line 2 Line 3 Line 4
  • 2. How to create Unicode PDF bookmarks using PDF::API2?
    Hi, I'm using PDF::API2 to add bookmarks to an existing PDF. It all works fine using outline->title($text) to name the bookmark except that any Unicode in $text is ignored.[1] What do I need to do to be able to create and display bookmarks that contain Unicode? Is there an API2 mailing list or similar that might be a better place to ask this question? Thanks in advance. [1] By ignored I mean, for example, that \x{2192} is displayed as two characters, presumably both in Latin1 or whatever it is that Reader uses for bookmarks by default. -- Bruce Horrocks Surrey England <firstname>@<surname>.plus.com -- fix the obvious for email

how to parse string from [?

Postby sam » Sun, 11 Apr 2004 01:51:10 GMT

Hi,

I have the following string need to be parsed in perl,
"sshd(pam_unix)[2009]"

I want to extract the first the word up to "[", what is the regular 
expression can do that?
The following regular express does not work:
"m/^([^\[]*)\[.*]$"

Thanks
sam

Re: how to parse string from [?

Postby Jim Gibson » Sun, 11 Apr 2004 02:14:12 GMT

On 2004-04-09 09:51:10 -0700, sam < XXXX@XXXXX.COM > said:


"does not work" is a little vague. You need to terminate the regular 
expression with another '/'. Except for that, your expression works for 
me. You can simplify it a little by 1) not escaping ']' in the 
character class (it doesn't need it), and 2) not including the 
extraneous '\[.*]$' at the end, unless you are trying to eliminate 
lines that do not match that part.

Please post a complete, working program and tell us what it is doing 
that "does not work".

-- 
Jim Gibson


Re: how to parse string from [?

Postby Gunnar Hjalmarsson » Sun, 11 Apr 2004 02:23:33 GMT



No? Exactly what did you expect it to do, and what did it do? Please 
post complete code.

(I take for granted that the missing trailing slash is a typo when you 
retyped the code into the message. Don't retype - copy and paste!)

-- 
Gunnar Hjalmarsson
Email:  http://www.**--****.com/ 


Re: how to parse string from [?

Postby sam » Sun, 11 Apr 2004 02:27:44 GMT




Thanks for your suggestion, here is my simplified perl script:
#!/usr/bin/perl -w
use strict; while (<DATA>) { print "$1\n" if "m/^([^\[]*)\[.*$/"; } __DATA__
sshd(pam_unix)[2009]

Note, there is no "\n" at the end of the "sshd(..)[2009]" string.

thanks
sam

Re: how to parse string from [?

Postby Paul Lalli » Sun, 11 Apr 2004 02:40:53 GMT






Why are you putting quotes around your whole regexp?  You're suddenly
asking if this big long string returns a true value, rather than whether
or not a pattern match succeeds.  Get rid of those quotes.

print "$1\n" if /^([^[]*)/;


Paul Lalli

Re: how to parse string from [?

Postby sam » Sun, 11 Apr 2004 02:58:53 GMT






Thanks, it works now.
sam

Re: how to parse string from [?

Postby Purl Gurl » Sun, 11 Apr 2004 03:51:11 GMT



(snipped)

 

My if conditional can be removed if your
input string is always in the format shown.
Different conditionals can be added if more
strict conditional parsing is needed.


Purl Gurl

TEST SCRIPT:
____________

#!perl

$input = "sshd(pam_unix)[2009]";

if (index ($input, "[") > -1)
 { print substr ($input, 0, index ($input, "[")); }


PRINTED RESULTS:
________________

sshd(pam_unix)

Similar Threads:

1.Parsing a string into an array

Hello

I am getting a strange error where I am trying to split a string into 
an array. The string is a line obtained from doing a directory of a 
disk. The error message is:

Reference to nonexistent group in regex; marked by <-- HERE in m/ 
Directory of C
:\Documents and Settings\Andrew\Application 
Data\ActiveState\KomodoIDE\4 <-- HER
E .1/ at D:\Perl Scripts\remove_duplicate_files.pl line 56.

The line in where it happens is the line of code with the split.

Thanks,
Andrew


#
# Find all the files
#
@file_list = `dir c: /S`;

#
# Build the list of directories and files
#
$temp_index = 0;

$file_index = 0;

$directory_index = 0;

$dir_list_size = scalar(@file_list);

print $dir_list_size;
print "\n:";

while ($temp_index < $dir_list_size)
{
    chomp @file_list[$temp_index];

    print @file_list[$temp_index];
    print "\n";

    if (@file_list[$temp_index] ne "")
    {
       #
       #
       @parse_line = split(@file_list[$temp_index]);

       # $line_size = scalar(@parse_line);
       $line_size = 0;

       if ($line_size > 0)
       {
          #print @parse_line[0];
          #print "\n";

          #
          # Determine if this is a directory and if so add it to the
          # directory array and increment the index
          #
          if (@parse_line[1] eq "Directory")
          {
             #$directory_list = @parse_line[1];
             #$directory_index =$directory_index + 1;
             #print "is a Directory\n";
          }
          else
          {
             ##$actual_files[file_index] = @parse_line[0];
             # $file_size[file_index] = @parse_line[4];
             #$file_deleted[file_index] = 0;

             #$file_index = $file_index + 1;
             #print "not a directory \n";
          }
       }
    }

    $temp_index = $temp_index + 1;
}
   

2.Parse String

I have the following code in Perl

$buf = "<tr><td class=yfnc_tablehead1 width="48%">Change:</td><td
class=yfnc_tabledata1><b style=color:#008800;>2.04
(0.66%)</b></td></tr>"

I am using the following code to get the value 2.04.
if ($buf =~ m/$Change/)
{
   ($Part1, $Part2) = split (/$Change/, $buf);
    # compare with the digit
   if ($Part2 =~ m/(\+|-)?[0-9]+\.[0-9]*/)
   {
     I do not know what to write here to get 2.04
   }
}
Can somebody help me out? I know this is the worst way to parse the
string. Is there some better way to solve it?

3.module for parsing a string into a formula

Sorry if I'm not saying this right...

I am coming from a C++ environment, that newsgroup suggested PERL as a
possible answer for my need. I have no experience with PERL at all, so will
have to do my homework if someone says there is a way.

I need a function ( module? ) that can take a string expression "(1 + 2) *
(3 + 4)" and return the answer of "21". It can be either a numerical value,
or a string value does not matter from this point.

Any pointers in the right direction would be welcome. A commercial software
package would be acceptable as well.

Thank you in advance

Michael

4.regex: parsing out varying length substr from varying string

I am trying to parse some HTML with a Perl script. The script is not
working correctly. The parsing portion of the script reads:

getDealers {
	my $URL = shift;
	my $fullString = get($URL);
	my $startIndex = 0;
	my $endIndex = 0;

	my $thisDealerURL;
	my $thisDealerTitle;

	# dealer URL
	my $urlMatch = 'Bookseller: <a
href=\"/servlet/BookDetailsPL\?bi=\d\d\d\d\d\d\d\d\d&tab=1&searchurl=bx%3Doff%26sts%3Dt%26ds%3D100%26bi%3D0%26isbn%3D0805074562';
	my $endUrlMatch = "\"";
	my $urlPrepend = $baseURL .
'/servlet/BookDetailsPL\?bi=\d\d\d\d\d\d\d\d\d&tab=1&searchurl=bx%3Doff%26sts%3Dt%26ds%3D100%26bi%3D0%26isbn%3D0805074562';

# dealer name
	my  $dealerMatch = ">";
	my  $endDealerMatch = "</A>";


	while (1) {
		print ".";
		# get the URL
		$startIndex = index($fullString,$urlMatch,$startIndex) +
length($urlMatch);
		if ($startIndex == (-1 + length($urlMatch)) ) {
			print "\n";
			last; # break!
		}
		$endIndex = index($fullString,$endUrlMatch,$startIndex);
		$thisDealerURL = $urlPrepend .
substr($fullString,$startIndex,($endIndex - $startIndex));
		$startIndex = $endIndex; # advance the starting index to where we
stopped

		# get the title
		$startIndex = index($fullString,$dealerMatch,$startIndex) +
length($dealerMatch);
		$endIndex = index($fullString,$endDealerMatch,$startIndex);
		$thisDealerTitle = substr($fullString,$startIndex,($endIndex -
$startIndex));
		$startIndex = $endIndex;

}

---------------------------------------------

Here's a sample of the HTML:

<td class="bookseller" width="25%">Bookseller: <a
href="/servlet/BookDetailsPL?bi=602397062&tab=1&searchurl=bx%3Doff%26sts%3Dt%26ds%3D100%26bi%3D0%26isbn%3D%252F0805074562">Snowbound
Books</a><br/>
      <span class="scndInfo">(Marquette, MI, U.S.A.)</span></td>

<td class="bookseller" width="25%">Bookseller: <a
href="/servlet/BookDetailsPL?bi=527331473&tab=1&searchurl=bx%3Doff%26sts%3Dt%26ds%3D100%26bi%3D0%26isbn%3D%252F0805074562">LDS
Heritage Books</a><br/>
      <span class="scndInfo">(Bountiful, UT, U.S.A.)</span></td>

I want the script to put the dealer names (in this case, "Snowbound
Books" and "LDS Heritage Books") into the variable $thisDealerTitle. I
am pulling the HTML fine; when I ask it to print the source, it does
so. But running the above script returns "0 dealers loaded." Can anyone
see where I've gone wrong? Mere hints are appreciated.

thanks,
matt

5.Parsing line with similar strings

I have the following code looking on a text file for the string
"DATE". The text file has several lines one by the name of ADMIT DATE
and another one called DATE.

I just need the line that has the string 'DATE' but with the regular
expression I keep getting both.

Is there a way to just pull the string that has DATE in it and drop
the one that has ADMIT DATE? I tried several combinations of regular
expression but none seem to give me the deried result.


Thanks in Advance,

Glenn


------------ CODE -------------------------

#!/usr/bin/perl

open(FH1, "<C:/developer/scripts/data/TestFile.txt")  or die "can't
open filename: $!";

@lines = <FH1>;

while ($line = pop @lines) {
	if ($line =~ m/^DATE/) {
        print $line;
	}
}



close(FH);





--------- TEXT FILE ------------

04-03-2007 DATE
04-03-2007 ADMIT DATE

6. Parse a String that probably really simple to do

7. Parsing/String Question

8. Module to parse CSV string



Return to PERL

 

Who is online

Users browsing this forum: No registered users and 7 guest