Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
3256 connectés 

  FORUM HardWare.fr
  Programmation
  Perl

  Doute sur une expression régulière

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Doute sur une expression régulière

n°1712137
stansoad01​08
Posté le 03-04-2008 à 14:15:19  profilanswer
 

Bonjour à tous,
 
Simple doute, donc simple question sur les regex :
Dans un de mes documents, je retrouve des phrases sous la forme : "Leu/Leu" ou "Pas/Pas" ou "Tir/Tir"..."Xxx/Xxx"
 
Je ne trouve pas une expression régulière pour les récupérer. Il faudrait dire
que les 2 chaînes de caractères sont identiques, commençant par une majuscule, composées de 3 lettre et séparées par un "/".
 
Il faudrait completer cette expression : (en faisant en sorte que ces 2 chaines de caractères soient identiques !!)
$S =~ /^[A-Z]{1}[a-z]{2}\/[A-Z]{1}[a-z]{2}$/
 
Merci pour vos commentaires

mood
Publicité
Posté le 03-04-2008 à 14:15:19  profilanswer
 

n°1712142
anapajari
s/travail/glanding on hfr/gs;
Posté le 03-04-2008 à 14:25:37  profilanswer
 

/^([A-Z][a-z]{2})\/\1$/


---------------
Software and cathedrals are much the same - first we build them, then we pray.
n°1712148
stansoad01​08
Posté le 03-04-2008 à 14:44:43  profilanswer
 

Merci pour ta réponse.
 
Mais cela dit, j'ai remarqué dans ma page html, les informations du type : "Xyz/Xyz"...sont ancrées dans des balises avec pour mise en forme :
 
Code :
 

Code :
  1. <td>
  2.            
  3.             Asp/Asp
  4.           </td>


 
On y retrouve des sauts de lignes et tabulations !! Pouvez vous ainsi m'aider pour créer l'expression régulière pour récupérer cette informations
 
Idées ???
Code :
 

Code :
  1. $s =~ m/^\n\n\t([A-Z]{1}[a-z]{2}\/[A-Z]{1}[a-z]{2})$/


 
ou
Code :
 

Code :
  1. $s =~ m/^\n\n\t([A-Z][a-z]{2})/\1


 
Merci


Message édité par stansoad0108 le 03-04-2008 à 14:45:20
n°1712161
anapajari
s/travail/glanding on hfr/gs;
Posté le 03-04-2008 à 15:09:09  profilanswer
 

vire le ^ (et le $ au passage) au debut de la regex, c'est lui qui te pose problème:

/([A-Z][a-z]{2})\/\1/


Message édité par anapajari le 03-04-2008 à 15:09:18

---------------
Software and cathedrals are much the same - first we build them, then we pray.
n°1712595
stansoad01​08
Posté le 04-04-2008 à 09:36:04  profilanswer
 

Bonjour,
 
J'ai bien rajouté ta réponse dans mon programme; cela fonctionne, mais dans ma console on m'imprime toujours :
 

Code :
  1. Amino Acid Translation :                           Leu/Leu


 
Je voudrais à présent enlever cette tabulation...
J'ai même essayé de rajouter ($text =~ s/\n/ /g;) pour tenter de supprimer les tabulations, mais cela m'affiche comme ci dessus
 
Pouvez vous m'aider
Merci

n°1712596
anapajari
s/travail/glanding on hfr/gs;
Posté le 04-04-2008 à 09:40:20  profilanswer
 

stansoad0108 a écrit :

Bonjour,
J'ai bien rajouté ta réponse dans mon programme; cela fonctionne, mais dans ma console on m'imprime toujours :

Code :
  1. Amino Acid Translation :                           Leu/Leu




fait voir le reste du code


---------------
Software and cathedrals are much the same - first we build them, then we pray.
n°1712688
stansoad01​08
Posté le 04-04-2008 à 11:34:16  profilanswer
 

D'accord, mais je te préviens, il est assez long...
 
Ce n'est pas si grave, si dans ma console, j'ai des tabulations....Donc ne te casses pas la tête pour ca !!!
 

Code :
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use HTML::Parser;
  5. use LWP::Simple;
  6. #Variables
  7. my $baseurl = 'http://www.pharmgkb.org/views/reports/loadVariantReports.action?geneId=';
  8. my $flag = 0;
  9. die "usage: $0 role code\n"
  10.     if @ARGV != 1;
  11. my $code = $ARGV[0];
  12. print "$code\n";
  13. #Page URL où parser
  14. my $url = $baseurl.$code;
  15. my $page = get($url);
  16. print "$url\n";
  17. #Tableau où sera ranger le menu :
  18. my @tab_menu=@_;
  19. #Parser
  20. my $parser = HTML::Parser->new(start_h => [\&start_rtn,"tag, attr"],
  21.                 text_h => [\&text_rtn, "text"],
  22.                 end_h => [\&end_rtn, "tag"]
  23.                 );
  24. #****************************************************************************************
  25. sub start_rtn {
  26. my ($tag, $attr) = @_;
  27. #$Flag = 1 (Titre)   
  28. if ($tag =~ /^title$/){
  29.          $flag = 1;
  30.     }
  31. #$Flag = 2 (Menu)
  32. if($tag =~ /^th$/){
  33.  $flag = 2;
  34. }
  35. #$Flag = 3 (GP Position)
  36. if ($tag =~ /^a$/
  37.  and defined $attr->{href} 
  38.       and $attr->{href} =~ /^\/redirect\.jsp\?p=http%3A%2F%2Fgenome\.ucsc\.edu%2Fcgi-bin%2FhgTracks%3Fposition%3D/
  39.      and defined $attr->{target}
  40.       and $attr->{target} =~ /^offsite$/){   
  41.     $flag = 3;
  42. }
  43. #$Flag = 4 (dbSNP Id)
  44. if ($tag =~ /^a$/
  45.  and defined $attr->{href} 
  46.       and $attr->{href} =~ /^\/redirect\.jsp\?p=http%3A%2F%2Fwww\.ncbi\.nlm\.nih\.gov%2Fentrez%2Fquery./
  47.      and defined $attr->{target}
  48.       and $attr->{target} =~ /^offsite$/){   
  49.     $flag = 4;
  50. }
  51. #$Flag = 5 (Variant)
  52. if ($tag =~ /^a$/
  53.  and defined $attr->{href}
  54.  and $attr->{href} =~ /^\/views\/reports\/loadVariantReport/){
  55.  $flag = 5;
  56. }
  57. #$Flag = 6 (Feature / Amino Acid Translation / Number of Chromosomes / Assay Types)
  58. if ($tag =~ /^td$/){
  59.  $flag = 6;
  60. }
  61. #$Flag = 8 (Frequency)
  62. if ($tag =~ /^a$/
  63.  and defined $attr->{href}
  64.  and $attr->{href} =~ /^\/views\/reports\/loadFrequencyInSampleSets/){
  65.  $flag = 7;           
  66. }
  67. #$Flag = 9 (View == FIN)
  68. if ($tag =~ /^a$/
  69.  and defined $attr->{href}
  70.  and $attr->{href} =~ /^\/views\/reports\/loadSampleAlleles/){
  71.  $flag = 9;           
  72. }
  73. }
  74. #*****************************************************************************************
  75. sub text_rtn {
  76. my ($text) = @_;
  77.     $text =~ s/\n/ /g;
  78. #$Flag = 1 (Titre)
  79. if($flag == 1){               
  80.          print "Le titre : $text \n";
  81.      }
  82. #$Flag = 2 (Menu)
  83. if($flag == 2){               
  84.  push(@tab_menu, $text);
  85.      }
  86. #$Flag = 3 (GP Position)
  87. if($flag == 3){             
  88.           print "GP POSITION : $text \n";
  89.      }
  90. #$Flag = 4 (dbSNP Id)
  91. if($flag == 4){
  92.  my $base_URL_dbSNP_Id = 'http://www.ncbi.nlm.nih.gov/sites/entrez?db=snp&cmd=search&term=';             
  93.  my $url_dbSNP_Id = $base_URL_dbSNP_Id.$text;
  94.  print "dbSNP Id : $text /// $url_dbSNP_Id\n";
  95.      }
  96. #$Flag = 5 (Variant)
  97. if($flag == 5 && $text =~ /^[^0-9]\/.+$/){
  98.  print "Variant : $text \n";
  99. }
  100. #$Flag = 6 (Feature)
  101. if($flag == 6 && (($text eq 'Exon') || ($text eq 'Intron') || ($text eq 'NA') || ($text =~ /^[0-9]' UTR$/))){
  102.  print "Feature : $text\n" ;
  103. }
  104. #$Flag = 6 (Amino Acid Translation)
  105. if($flag == 6 && ($text =~ /^[A-Z]{1}[a-z]{2}\/[A-Z]{1}[a-z]{2}$/) || ($text =~ /^[A-Z]{1}[a-z]{2}$/) || ($text =~ /^\s[^0-9]+\/[^0-9]+$/)){
  106.   $text =~ s/\n/ /g;
  107.   print "Amino Acid Translation : $text\n";
  108. }
  109. #$Flag = 7 (Frequency)
  110. if($flag == 7){
  111.  print "Frequency : $text\n";
  112.      }
  113. #$Flag = 6 (Number of Chromosomes)
  114. if($flag == 6 && ($text =~ /^[0-9]/ && $text ne '3\' UTR' && $text ne '5\' UTR')){
  115.  print "Number of Chromosomes : $text\n";
  116. }
  117. #Flag = 6 (Assay Types)
  118. if($flag == 6 && (($text =~ /^[A-Z]{1}[a-z]{3,}$/ || $text =~ /^[A-Z]{1}[a-z]{3,}, [A-Z]{1}[a-z]{3,}$/) && $text ne 'Exon' && $text ne 'Intron'
  119. && $text ne 'View' && $text ne 'Queries' && $text ne 'Drugs' && $text ne 'Genes' && $text ne 'Diseases' && $text ne 'Pathways')) {
  120.  print"Assay types : $text\n";
  121. }
  122. #$Flag = 9 (View == FIN)
  123. if($flag == 9){
  124.  print"* * * * * * * * * * * * * *\n";
  125. }
  126. }
  127. #******************************************************************************************
  128. sub end_rtn {
  129. my ($tag) = @_;
  130. #$Flag = 1 (Titre)
  131. if ($tag =~ /^\/title$/){
  132.                $flag = 0;
  133. }
  134. #$Flag = 2 (Menu)
  135. if ($tag =~ /^\/thead$/){
  136.       my $Text_menu = join("\n ", @tab_menu);
  137.       print"===> MENU : \n $Text_menu\n";
  138.  $flag = 0;
  139.  print "****************\n";
  140. }
  141. #$Flag = 3 (GP Position)
  142. if ($tag =~ /^\/a$/ && ($flag == 3)){
  143.                $flag = 0;
  144. }
  145. #$Flag = 4 (dbSNP Id)
  146. if ($tag =~ /^\/a$/ && ($flag == 4)){
  147.                $flag = 0;
  148. }
  149. #$Flag = 5 (Variant)
  150. if ($tag =~ /^\/a$/ && ($flag == 5)){
  151.                $flag = 0;
  152. }
  153. #$Flag = 6 (Feature / Amino Acid Translation / Number of Chromosomes / Assay Types)
  154. if ($tag =~ /^\/td$/ && ($flag == 6)){
  155.  $flag = 0;
  156. }
  157. #$Flag = 8 (Frequency)
  158. if ($tag =~ /^\/a$/ && ($flag == 7)){
  159.                $flag = 0;
  160. }
  161. #$Flag = 9 (View == FIN)
  162. if ($tag =~ /^\/a$/ && ($flag == 9)){
  163.                $flag = 0;
  164. }
  165. }
  166. #*******************************************************************************************
  167. #start parsing
  168. $parser->parse($page);
  169.  
  170. #end parser
  171. $parser->eof;
  172. #FIN PROGRAMME !!!


 
 
Et je te donne une partie de ma console finale :
 

Code :
  1. PA50
  2. http://www.pharmgkb.org/views/repo [...] eneId=PA50
  3. Le titre : PharmGKB: Variants in APOB
  4. ===> MENU :
  5. GP Position
  6.  
  7. dbSNP Id
  8.  
  9. Variant
  10.  
  11. Feature
  12.  
  13. Amino Acid
  14. Translation
  15.  
  16. Frequency
  17.  
  18. Number of
  19. Chromosomes
  20.  
  21. Assay Types
  22.  
  23. Flags
  24.  
  25. Data
  26. ****************
  27. GP POSITION : chr2:21077927
  28. dbSNP Id : rs12720763 /// http://www.ncbi.nlm.nih.gov/sites/ [...] rs12720763
  29. Variant : G/T
  30. Feature : 3' UTR
  31. Frequency : 98.89%/1.11%
  32. Number of Chromosomes : 90
  33. Assay types : Sequencing
  34. * * * * * * * * * * * * * *
  35. GP POSITION : chr2:21078348
  36. dbSNP Id : rs12713450 /// http://www.ncbi.nlm.nih.gov/sites/ [...] rs12713450
  37. Variant : C/T
  38. Feature : Exon
  39. Amino Acid Translation : Thr/Met
  40. Frequency : 97.87%/2.13%
  41. Number of Chromosomes : 94
  42. Assay types : Sequencing
  43. * * * * * * * * * * * * * *
  44. GP POSITION : chr2:21079015
  45. dbSNP Id : rs12713457 /// http://www.ncbi.nlm.nih.gov/sites/ [...] rs12713457
  46. Variant : C/T
  47. Feature : Exon
  48. Amino Acid Translation :                           Leu/Leu         
  49. Frequency : 98.94%/1.06%
  50. Number of Chromosomes : 94
  51. Assay types : Sequencing
  52. * * * * * * * * * * * * * *
  53. .....
  54. .....
  55. .....

n°1712787
anapajari
s/travail/glanding on hfr/gs;
Posté le 04-04-2008 à 14:45:10  profilanswer
 

Tout d'abord tu n'as pas utilisé la regex que je conseillais c'est dommage par exemple "Leu/Too" sera matché alors que ça devrait pas.
La tienne est vraiment pas terrible en plus.

 

Ensuite, tu n'affiches pas la partie de texte qui correspond à ta recherche mais l'intégralité de "la ligne" où ta pattern a été rencontrée.
Par exemple si tu as dans $text:

toto titi tata Leu/Leu tiitii taataa tootoo


et bien tu afficheras

Amino Acid Translation : toto titi tata Leu/Leu tiitii taataa tootoo


c'est ce morceau de code qui va pas:

Code :
  1. #$Flag = 6 (Amino Acid Translation)
  2. if($flag == 6 &&
  3. (
  4.  $text =~ /^[A-Z]{1}[a-z]{2}\/[A-Z]{1}[a-z]{2}$/) ||
  5.  $text =~ /^[A-Z]{1}[a-z]{2}$/ ||
  6.  $text =~ /^\s[^0-9]+\/[^0-9]+$/
  7. )
  8. ){
  9.  $text =~ s/\n/ /g;
  10.  print "Amino Acid Translation : $text\n";
  11. }


Le plus simple reste peut être de virer les tabs de $text au même endroit que tu enlèves les retours à la ligne (l96):

Code :
  1. $text =~ s/[\n\t]//g;
 

note: tout ça pourrait être ré-écrit en 5 fois moins de ligne et 10 fois plus clair, mais vu que ça a l'air de faire ce que tu veux on va pas se compliquer la vie ;)


Message édité par anapajari le 04-04-2008 à 14:46:53

---------------
Software and cathedrals are much the same - first we build them, then we pray.
n°1714374
stansoad01​08
Posté le 08-04-2008 à 14:39:20  profilanswer
 

C'est bon j'ai rajouté ton expression dans ma sub text_rtn..Ca fonctionne
 
Merci


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Perl

  Doute sur une expression régulière

 

Sujets relatifs
[PHP] contrôle de saisie - expression régulièresExpression reguliere - je ne comprends pas
Expression conditionnellecomment ecrire après une expression lue avec lex?
Probleme expression reguliereImprimer les lignes avant et apres l'expression rechercher
Expression régulière pour moteur de recherche... 
Plus de sujets relatifs à : Doute sur une expression régulière


Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)