So, I'm back asking your help. I hope I don't make too many mistakes posting this.
My problem is this: I have a .txt file, which is like this:
L'/lo/RDNS harmonium/harmonium/S Ë/essere/V-S3IP uno/uno/RIMS stru +mento/strumento/S-MS musicale/musicale/A-NS azionato/azionare/V-MSP +R con/con/E una/una/RIFS tastiera/tastiera/S-FS
which is the result of a POS tagging.
I also have a list of couple of word, that I made, which looks like this one:
[Nn]ucle[oi]/nucleo/S-MS:[Pp]roton[oi]/protone/S-MP [Aa]tom[oi]/atomo/S-MS:[Nn]ucle[oi]/nucleo/S-MS
I need to do something that takes both file as input and print me everything that's between the first "word" which is really a string and the second one.
I tried this code:
#!/usr/bin/perl use strict; use warnings; open my $listaParole,"<File_with_the_words" or die; my %hash; while (my $line=<$listaParole>) { chomp $line; my ($word1, $word2) = split /:/, $line; $hash{$word1} = $word2; } while ( my ($k,$v) = each %hash ) { print "Key $k => $v\n"; } open my $testo, "<File_with_the_text"; open my $lista_relazioni, ">Output"; my @arrayris =() ; my $indice=0; while (my $text=<$testo>){ for my $key (keys %hash){ my $value = $hash{$key}; while ($text =~ / $key (.*)? $value /g) { $arrayris[$indice]=$1; $indice++; } } } my $indice_controllo=1; foreach (@arrayris) { print $lista_relazioni "($indice_controllo) $_\n"; $indice_controllo++; } close $testo; close $lista_relazioni;
but obviously the regex ($text =~ / $key (.*)? $value /g) isn't too accurate. I would like for it to match just string that have the first "word" than 3 or 4 other "words" and than the second one. Is it possible? How should I do it? Is the rest of the code any good? Thanks for your help.

In reply to Regex matching by b_vulnerability

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.