2011-08-05 2 views
0

J'essaie d'obtenir une expression régulière pour trouver plusieurs entrées de mon motif sur une ligne. Note: J'utilise Regex pendant environ une heure ... =/J'ai besoin d'aide pour obtenir l'expression Regex correcte

Par exemple:

<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 

doit correspondre à deux fois:

1) <a href="G2532" id="1">back</a> 
2) <a href="G2564" id="2">next</a> 

Je pense que la réponse se trouve dans la bonne Maîtrise de cupide vs réticent vs possessif mais je ne peux pas sembler le faire fonctionner ...

Je pense que je suis proche, la chaîne Regex que j'ai créé jusqu'à présent est:

(<a href=").*(" id="1">).*(</a>) 

Mais le matcher Regex retourne 1 match, l'ensemble de la chaîne ...

J'ai un (compilable) Java Regex Harnais de test dans le code ci-dessous. Voici mes tentatives récentes (futiles) pour l'obtenir en utilisant ce programme, la sortie devrait être assez intuitive.

Enter your regex: (<a href=").*(" id="1">).*(</a>) 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

Enter your regex: (<a href=").*(" id="1">).*(</a>)? 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

Enter your regex: (<a href=").*(" id="1">).*(</a>)+ 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

Enter your regex: (<a href=").*(" id="1">).*(</a>)? 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

Enter your regex: ((<a href=").*(" id="1">).*(</a>))? 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 
I found the text "" starting at index 63 and ending at index 63. 

Enter your regex: ((<a href=").*(" id="1">).*(</a>))+? 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

Enter your regex: (((<a href=").*(" id="1">).*(</a>))+?) 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

Voici le Java:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.regex.Pattern; 
import java.util.regex.Matcher; 

public class RegexTestHarness { 

    public static void main(String[] args){ 
     try{ 
      while (true) { 

       System.out.print("\nEnter your regex: "); 

       BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
       Pattern pattern = Pattern.compile(reader.readLine()); 

       System.out.print("Enter input string to search: "); 
       Matcher matcher = pattern.matcher(reader.readLine()); 

       boolean found = false; 
       while (matcher.find()) { 
        System.out.println("I found the text \"" + matcher.group() + "\" starting at " + 
         "index " + matcher.start() + " and ending at index " + matcher.end() + "."); 
        found = true; 
       } 
       if(!found){ 
        System.out.println("No match found."); 
       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
      System.exit(-1); 
     } 

    } 
} 
+1

[Vous ne devriez pas essayer d'analyser HTML avec RegEx] (http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Bohemian

+0

In votre 'href' et entre vos balises' ', n'attendez-vous que des lettres/chiffres? – Josh

Répondre

1

Essayez ceci:

<a href=".*?" id="1">.*?</a> 

J'ai converti les captures à la non-gourmande en ajoutant un ? après .*

Mais quand dans le doute, vous pouvez utiliser cette astuce:

<a href="[^"]*" id="1">[^<]*</a> 

[^"]* signifie un certain nombre de caractères qui ne sont pas un guillemet
[^<]* signifie un certain nombre de caractères qui ne sont pas un angle gauche

Vous évitez se soucier gourmand/non gourmand

+0

Bohème, Tu m'as mis sur la bonne voie. J'ai utilisé votre technique, mais je me suis rendu compte que je devais changer l'id = "1" en "id =" [1-9] + ".Au final, ça marche maintenant, merci. – Ryan