Domanda

Il mio compito è quello di escogitare un'espressione regolare che riconoscerà l'articolo indefinito in inglese - la parola "a" o "an" IE per scrivere un'espressione regolare per identificare la parola a o la parola an. Devo testare l'espressione scrivendo un driver di prova che legge un file contenente circa dieci righe di testo. Il tuo programma dovrebbe contare le occorrenze delle parole "a" e "an". Non corrisponderò ai personaggi A e un in parole come thun.

Questo è il mio codice finora:

import java.io.IOException;
import java.util.Arrays;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegexeFindText {
   public static void main(String[] args) throws IOException {

      // Input for matching the regexe pattern
       String file_name = "Testing.txt";

           ReadFile file = new ReadFile(file_name);
           String[] aryLines = file.OpenFile();  
           String asString = Arrays.toString(aryLines);

            // Regexe to be matched
               String regexe = ""; //<<--this is where the problem lies

           int i;
           for ( i=0; i < aryLines.length; i++ ) {
           System.out.println( aryLines[ i ] ) ;
           }


      // Step 1: Allocate a Pattern object to compile a regexe
      Pattern pattern = Pattern.compile(regexe);
      //Pattern pattern = Pattern.compile(regexe, Pattern.CASE_INSENSITIVE);  
      // case-        insensitive matching

      // Step 2: Allocate a Matcher object from the compiled regexe pattern,
      //         and provide the input to the Matcher
      Matcher matcher = pattern.matcher(asString);

      // Step 3: Perform the matching and process the matching result

      // Use method find()
      while (matcher.find()) {     // find the next match
         System.out.println("find() found the pattern \"" + matcher.group()
               + "\" starting at index " + matcher.start()
               + " and ending at index " + matcher.end());
      }

      // Use method matches()
      if (matcher.matches()) {
         System.out.println("matches() found the pattern \"" + matcher.group()
               + "\" starting at index " + matcher.start()
               + " and ending at index " + matcher.end());
      } else {
         System.out.println("matches() found nothing");
      }

      // Use method lookingAt()
      if (matcher.lookingAt()) {
         System.out.println("lookingAt() found the pattern \"" + matcher.group()
               + "\" starting at index " + matcher.start()
               + " and ending at index " + matcher.end());
      } else {
         System.out.println("lookingAt() found nothing");
      }
   }
}

La mia domanda è semplicemente cosa devo usare per trovare quelle parole nel mio testo? Qualsiasi aiuto sarebbe molto apprezzato, grazie!

È stato utile?

Soluzione

Ecco il regex che corrisponderà a "A" o "an":

String regex = "\\ban?\\b";

Abbattiamo quel regex:

  • \b significa confine di parole (è scritta una singola back slash come "\\" in java)
  • a è semplicemente un letterale "a"
  • n? significa zero o uno letterale "n"
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top