SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [PERL], find patterns and replace them by new values

    Hi all,

    My string
    (champs1 (champs6 donnee_o donnee_f) [(champs2 [] (champs3 _YOJNJeyyyyyyB (champs4 donnee_x)) (debut 144825 25345) (fin 244102 40647)), (champs2 [] (champs3 _FuGNJeyyyyyyB (champs4 donnee_z)) (debut 796443 190570) (fin 145247 42663))] [] []).
    In the above string, i would like to replace the integer values, respectively by these values:
    $moyLargRectNom, $moyHautRectNom, $moyLargRectNom, $moyHautRectNom, $moyLargRectMat, $moyHautRectMat, $moyLargRectMat, $moyHautRectMat
    This is my REGEX
    $ligne =~ s/(.*debut)\s\d+\s\d+(.*fin)\s\d+\s\d+(.*debut)\s\d+\s\d+(.*fin)\s\d+\s\d+(.*)/$1$moyLargRectNom$2$moyHautRectNom$3$moyLargRectNom$4$moyHautRectNom$5$moyLargRectMat$6$moyHautRectMat$7$moyLargRectMat$8$moyHautRectMat$9/;
    It doesn't replace the values at all; can anyone help me please? Thank you.

  2. #2
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the regexp there are 5 sets of capturing parenthsis ():

    Code:
    $ligne =~ s/(.*debut)\s\d+\s\d+(.*fin)\s\d+\s\d+(.*debut)\s\d+\s\d+(.*fin)\s\d+\s\d+(.*)/$1$moyLargRectNom$2$moyHautRectNom$3$moyLargRectNom$4$moyHautRectNom$5$moyLarg RectMat$6$moyHautRectMat$7$moyLargRectMat$8$moyHautRectMat$9/;
    so you will capture $1, $2, $3, $4 and $5 in memory. But in the replacement side of your regexp you go up to $9. $6 thru $9 will be undefined.

    Do all those scalar variables you posted have values? What are they?

  3. #3
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Kevin,

    my aim is to replace the first and second value after the word "debut", the first and second value after the word "fin" and so on for the second word "debut" and the second word "fin".
    For example, assuming that my string is the following, and that i would like to replace the values:
    (champs1 (champs6 donnee_o donnee_f) [(champs2 [] (champs3 _YOJNJeyyyyyyB (champs4 donnee_x)) (debut 144825 25345) (fin 244102 40647)), (champs2 [] (champs3 _FuGNJeyyyyyyB (champs4 donnee_z)) (debut 796443 190570) (fin 145247 42663))] [] []).
    If i've
    $MoyLargRectNom=109, $MoyHautRectNom=97, $MoyLargRectMat=924 and $MoyHautRectMat=3378
    , then my string will become:

    (champs1 (champs6 donnee_o donnee_f) [(champs2 [] (champs3 _YOJNJeyyyyyyB (champs4 donnee_x)) (debut 109 97) (fin 109 97)), (champs2 [] (champs3 _FuGNJeyyyyyyB (champs4 donnee_z)) (debut 924 3378) (fin 924 3378))] [] []).

  4. #4
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I understand your requirements:

    Code:
    $ligne = '(champs1 (champs6 donnee_o donnee_f) [(champs2 [] (champs3 _YOJNJeyyyyyyB (champs4 donnee_x)) (debut 144825 25345) (fin 244102 40647)), (champs2 [] (champs3 _FuGNJeyyyyyyB (champs4 donnee_z)) (debut 796443 190570) (fin 145247 42663))] [] []).';
    print $ligne,"\n";
    my ($FirstPart, $SecondPart) = split(/,/,$ligne);   
    
    $MLRN = 109;
    $MHRN = 97;
    $MLRM = 924;
    $MHRM = 3378;
    
    $FirstPart  =~ s/(\(debut \d+ \d+\) \(fin \d+ \d+\)\))/(debut $MLRN $MHRN) (fin $MLRN $MHRN)/;
    $SecondPart =~ s/(\(debut \d+ \d+\) \(fin \d+ \d+\)\))/(debut $MLRM $MHRM) (fin $MLRM $MHRM)/;
    
    $ligne = join(",",$FirstPart,$SecondPart);
    print $ligne;

  5. #5
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your line has many more parts that need the substitution and you are never sure how many parts there are, and assuming the comma "," can be used to seperate the line into tokens, you can do something like this:

    Code:
    $ligne = '(champs1 (champs6 donnee_o donnee_f) [(champs2 [] (champs3 _YOJNJeyyyyyyB (champs4 donnee_x)) (debut 144825 25345) (fin 244102 40647)), (champs2 [] (champs3 _FuGNJeyyyyyyB (champs4 donnee_z)) (debut 796443 190570) (fin 145247 42663))] [] []).';
    print $ligne,"\n";
    my @tokens = split(/,/,$ligne);   
    
    $MLRN = 109;
    $MHRN = 97;
    $MLRM = 924;
    $MHRM = 3378;
    
    foreach my $i (0..$#tokens){
       if ($i%2) {
          $tokens[$i] =~ s/(\(debut \d+ \d+\) \(fin \d+ \d+\)\))/(debut $MLRM $MHRM) (fin $MLRM $MHRM)/;
       }
       else {
          $tokens[$i] =~ s/(\(debut \d+ \d+\) \(fin \d+ \d+\)\))/(debut $MLRN $MHRN) (fin $MLRN $MHRN)/;
       }
    }
    $ligne = join(",",@tokens);
    print $ligne;


Bookmarks

Posting Permissions

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