getTagContent

Stato
Discussione chiusa ad ulteriori risposte.

orakool

Utente Electrum
30 Giugno 2007
323
6
2
118
È domenica mattina e non ho un pazzo da fare, così ho deciso di scrivere un po' di ruby.

L'esercizio è semplice: prendi in input un file html e un tag, restituisci il contenuto del tag.

in ruby:
http://sprunge.us/ahCQ?rb

Codice:
#! /usr/bin/env ruby
        
def getTagContent (file, tag) File.open(file).read.match(tag).post_match.match(tag.sub(/</,'</')).pre_match end

puts getTagContent('prova.html', '<body>')
Mi raccomando di dargli il nome esatto del file, non faccio alcun controllo sull'esistenza e vi offenderebbe con un'eccezione ^^
 
Da quant'era che non scrivevo in Perl :heart:

http://sprunge.us/BiXR?pl

Codice:
#!/usr/bin/perl

sub getTagContent ($$)
{
	my ($file, $tag) = @_;

	open FILE, "<$file";
	$lol .= $_ while (<FILE>);

	my $content =$1 if $lol =~ m#<$tag.*?>(.+?)</$tag>#s;

	close FILE;
	return $content;
}

print getTagContent ( "prova.html", "body" ) . "\n"
 
orakool ha detto:
L'esercizio è semplice: prendi in input un file html e un tag, restituisci il contenuto del tag.

mettiamo solo la funzione per il tag, in C++
(chissà dove è che l'ho usata :p )

Codice:
string getTagContent( string tag, string& data ) {
	string ret;
	size_t p[2];

	p[0] = data.find( tag );
	tag.insert(1,"/");
	p[1] = data.find( tag );

	if( p[0] != string::npos ) {
		ret = data.substr( p[0]-1+(tag.length()),p[1]+1-(p[0]+(tag.length())) );
	} else {
		ret = "";
	}

	return ret;
}


http://github.com/tilde/liblyrics/blob/master/src/crawler.cpp


L'avevo scritta io, quindi nn rompete
 
orakool ha detto:
Malex vedi? A fare i progetti con me è TUTTO DI GUADAGNATO, già già >.>

[ot] non sono io che li faccio con te, sono le mie idee e capacità che straripano da tutte le parti e per eccesso finiscono in mezzo ai tuoi coducoli da 4 soldi :p [/ot]
 
Stato
Discussione chiusa ad ulteriori risposte.