[Perl] $-[0] en $+[0] ???

Pagina: 1
Acties:

  • Sensei_D
  • Registratie: Maart 2002
  • Laatst online: 26-05 08:43
Ik bezig met reguliere expressies in Perl en het gaat mij vooral om de positie van een reguliere expressie in mijn opgegeven string...
Dit is wat ik heb om mijn komende vraag te verduidelijken:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
use strict;

my $String = "CACTTGGTCACTTGATGCATGACTGGTATAATGGCCACTTGGTCACTTGATGCTGACTGGTATAATGGC";

my $PromRegexp = "((TTG)([ATGC]{19,21})(TATAAT))|
                  ((TTGAC)([ATGC]{17,19})(TA[ATCG]{2}AT))|
                  ((TTGAC[ATGC]T)([ATGC]{16,18})(ATA[ATCG]T))|
                  (((TTGAC([ATCG]))|(TT[ATCG]ACA))([ATGC]{16,18})(TAT[AGCT]AT))|
                  ((TTGAC)([ATGC]{18,20})(ATAAT))" ;

while ($String =~ /$PromRegexp/g) {
    print $&, "\t", $-[0],"\t", $+[0],"\n";
}

Ik snap het bovenstaande tot de laatste printopdracht. Iemand heeft me laten zien dat je met $-[0] het beginpunt van de gevonden substring kon terughalen en met $+[0] het einde daarvan.

Nu is het probleem dat ik niet weet wat die "$-[0]" en "$+[0]" eigenlijk betekenen en op internet kan ik hier niets over terugvinden en ook nergens op dit forum...

Kan iemand me vertellen wat die 2 dingen zijn?

sensei_d.fpv channel


  • Soultaker
  • Registratie: September 2000
  • Laatst online: 04:03
man perlvar:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
       @LAST_MATCH_END
       @+      This array holds the offsets of the ends of the last successful
               submatches in the currently active dynamic scope.  "$+[0]" is
               the offset into the string of the end of the entire match.
               This is the same value as what the "pos" function returns when
               called on the variable that was matched against.  The nth ele-
               ment of this array holds the offset of the nth submatch, so
               "$+[1]" is the offset past where $1 ends, "$+[2]" the offset
               past where $2 ends, and so on.  You can use "$#+" to determine
               how many subgroups were in the last successful match.  See the
               examples given for the "@-" variable.

       @LAST_MATCH_START
       @-      $-[0] is the offset of the start of the last successful match.
               "$-["n"]" is the offset of the start of the substring matched
               by n-th subpattern, or undef if the subpattern did not match.

               Thus after a match against $_, $& coincides with "substr $_,
               $-[0], $+[0] - $-[0]".  Similarly, "$"n coincides with "substr
               $_, $-["n"], $+["n"] - $-["n"]" if "$-["n"]" is defined, and $+
               coincides with "substr $_, $-[$#-], $+[$#-]".  One can use
               "$#-" to find the last matched subgroup in the last successful
               match.  Contrast with "$#+", the number of subgroups in the
               regular expression.  Compare with "@+".

               This array holds the offsets of the beginnings of the last suc-
               cessful submatches in the currently active dynamic scope.
               "$-[0]" is the offset into the string of the beginning of the
               entire match.  The nth element of this array holds the offset
               of the nth submatch, so "$+[1]" is the offset where $1 begins,
               "$+[2]" the offset where $2 begins, and so on.  You can use
               "$#-" to determine how many subgroups were in the last success-
               ful match.  Compare with the "@+" variable.

               After a match against some variable $var:

               ""$`"" is the same as ""substr($var, 0, $-[0])""
               ""$&"" is the same as ""substr($var, $-[0], $+[0] - $-[0])""
               ""$'"" is the same as ""substr($var, $+[0])""
               ""$1"" is the same as ""substr($var, $-[1], $+[1] - $-[1])""
               ""$2"" is the same as ""substr($var, $-[2], $+[2] - $-[2])""
               ""$3"" is the same as ""substr $var, $-[3], $+[3] - $-[3])""

Was dat nou echt zo moeilijk te vinden?

  • Sensei_D
  • Registratie: Maart 2002
  • Laatst online: 26-05 08:43
OMG! Ik heb het echt niet kunnen vinden. Wauw thnx man _/-\o_

sensei_d.fpv channel


  • Soultaker
  • Registratie: September 2000
  • Laatst online: 04:03
Kijk in het vervolg eerst even op PerlDoc.com (daar was ook de informatie over @+ en @- te vinden).