www.webdeveloper.com
Results 1 to 2 of 2

Thread: RegExp won't match :(

  1. #1
    Join Date
    Jan 2005
    Location
    Oslo, Norway
    Posts
    32

    RegExp won't match :(

    Hello all

    I'm puzzling around with a script which can "translate" internet pages using a dictionary by using RegExp substituting. The only problem is that I don't want links or sources of images to be translated because then they would not point to correct and valid pages.

    I've found a way to avoid this, but it doesn't work (So much for my idea :P)

    Code:
    open WORDS, "../dict.txt"
    	or die "Couldn't open txt file :(";
    
    	for $word (@wordsin = <WORDS>)
    	{
    		my $temp;
    		my @temp = split/&/, $word;
    		$words{$temp[0]} = $temp[1];
    	}
    
    close WORDS;
    
    my $key;
    my $finishedcont = get($url);
    
    foreach $key (keys %words)
    {
    	#For each word, substitute every occurence with the translated value surrounded by the string "123"
    	$finishedcont =~ s{$key}{123$words{$key}123}gis;
    	
    	#Now change every occurence of the current translated word surrounded by 123 inside =" " (Typical links or image sources) back to the original
    	$finishedcont =~ s{="(.*)?(123$words{$key}123)(.*)?"}{="$1$key$3"}gis;
    	
    	#Strip of the surrounding 123's from the untouched translations
    	$finishedcont =~ s{123$words{$key}123}{$words{$key}}gis;
    }
    In the dictionary, the words are written as such:
    original_word&translated_word
    I then assign this to a hash where the key is the original word, and the value is the translated one.

    My problem here is that the middle substitute doesn't work... Nothing is translated back, even though it is a link with the correct formatting...

    Anyone know where I've screwed up?

    Thanks in advance
    Johnoo
    Relax, everything is going perfectly out of control

    The great thing about a computer notebook is that no matter how much you stuff into it, it doesn't get bigger or heavier.
    Bill Gates (1955 - ), Business @ The Speed of Thought

  2. #2
    Join Date
    Mar 2006
    Posts
    9
    What do you mean by "middle substitute"? Try getting a plain text file to translate properly first. Then evolve your script to handle html files.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles