a simple =~ // question

G

Geoff Cox

Hello,

If I have

<title>
fred - jane
</title>

how can I find the fred - jane part?

I have tried

if ($line =~ /<title>(.*?)</title>/s) {

print $1;

}

but it is not working ...

Thanks

Geoff
 
M

Mark Clements

Geoff said:
Hello,

If I have

<title>
fred - jane
</title>

how can I find the fred - jane part?

I have tried

if ($line =~ /<title>(.*?)</title>/s) {

print $1;

}

but it is not working ...

I'm not surprised:

Bareword found where operator expected at 1 line 1, near
"/<title>(.*?)</title"
(Missing operator before title?)
syntax error at 1 line 1, near "/<title>(.*?)</title"
Search pattern not terminated at 1 line 1.

Did you actually try to compile this?

You need to escape the slash in your pattern.

perldoc perlre

Also, if you are trying to parse XML or HTML there are better methods
than hand-rolling your own regex.

search.cpan.org

Mark
 
T

Tad McClellan

Geoff Cox said:
If I have

<title>
fred - jane
</title>

how can I find the fred - jane part?


my $position = index $_, 'fred - jane';
print "the 'fred - jane' part is at char number $position\n";
 
G

Geoff Cox

my $position = index $_, 'fred - jane';
print "the 'fred - jane' part is at char number $position\n";

Thanks to both - I also remembered using

if ( $line =~ /^<title>$/ ) {

my ($next) = <IN>;

and $next gets the fred - jane text

Cheers

Geoff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top