Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern Matching 1

Status
Not open for further replies.

ETbyrne

Programmer
Dec 27, 2006
59
US
Hi, I want to go threw a file until a line matches a pattern. It then prints that line and exits. The pattern I want to match is: <item>anything</item>

Her is my script so far.

Code:
#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);
use Fcntl qw(:flock :seek);

open(FILE, ">>feed.rss") or die "Can not open feed.rss: $!";
seek(FILE,0,0);
my @file = <FILE>;

foreach (@file)
{
  if ($_ =~ /some pattern/) # Help!
  {
    print "$_";
    print "<br>Matched!";
    exit;
  }
}

close FILE;
exit;

As you can see I don't know how to do the pattern matching. Remember I want it to match: <item>anything</item>

anything could be numbers, letters, spaces or any combination.

Thanks for all help!

_______________________________________

You don't know me.
 
oops, this one:

Code:
if ($_ =~ /<item>[^>]*</item>/)

should be:

Code:
if ($_ =~ /<item>[^<]*</item>/)



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
it's going to be one of those days for me:

Code:
if ($_ =~ /<item>.*?<\/item>/)

or

Code:
if ($_ =~ /<item>[^<]*<\/item>/)

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ok, I used both of them (that is the last two ones you posted) but it won't match <item>test</item> or anything else like that.

_______________________________________

You don't know me.
 
the regexps work fine:

Code:
my @file = ('<blah>foo</blah>','<item>foo bar</item>','<foo>bar baz</foo>');
foreach (@file)
{
  if ($_ =~ /<item>.*?<\/item>/)
  {
    print "Matches <item>.*?<\/item>\n";
  }
  if ($_ =~ /<item>[^<]*<\/item>/)
  {
    print "Matches: <item>[^<]*<\/item>\n";
  }
}

probably you're trying to match something different than what you posted.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I found out what it was. It was that I had to change

Code:
open(FILE, ">>feed.rss");

to

Code:
open(FILE, "+<feed.rss")

_______________________________________

You don't know me.
 
Hi ETbyrne,

If all you're doing is processing a file's content, why bother opening it in read AND write mode. Just keep it simple:

Code:
[gray]#!/usr/bin/perl -w[/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]CGI[/green] [red]qw([/red][purple]:standard[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]FILE, [red]"[/red][purple]feed.rss[/purple][red]"[/red][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open feed.rss: [blue]$![/blue][/purple][red]"[/red][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<FILE>[red])[/red] [red]{[/red]
	[olive][b]if[/b][/olive] [red]([/red][red]m{[/red][purple]<item>.*?</item>[/purple][red]}[/red][red])[/red] [red]{[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$_[/blue]<br>Matched![/purple][red]"[/red][red];[/red]
		[olive][b]last[/b][/olive][red];[/red]
	[red]}[/red]
[red]}[/red]

[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url] FILE[red];[/red]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top