Pregunta

Mi tarea es diseñar una expresión regular que reconozca el artículo indefinido en inglés: la palabra "a" o "un" es decir, escribir una expresión regular para identificar la palabra a o la palabra an. Debo probar la expresión escribiendo un controlador de prueba que lea un archivo que contiene aproximadamente diez líneas de texto. Su programa debe contar las ocurrencias de las palabras "A" y "An".un.

Este es mi código hasta ahora:

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");
      }
   }
}

Mi pregunta es simplemente ¿qué tengo que usar para encontrar esas palabras dentro de mi texto? Cualquier ayuda sería muy apreciada, ¡gracias!

¿Fue útil?

Solución

Aquí está el Regex que coincidirá con "A" o "An":

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

Rompamos ese Regex:

  • \b significa límite de palabras (un solo corte de espalda se escribe como "\\" en Java)
  • a es simplemente un literal "a"
  • n? significa cero o uno literal "n"
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top