...La classe « java.lang.String » en Java contient déjà un support assez avancé pour les expressions régulières. On peut ainsi vérifier la concordance d’un motif, séparer une chaîne de caractères en sous-chaînes en utilisant des motifs et, finalement, utiliser la fonction rechercher/remplacer...
String class matches method example:- This example demonstrates the working of matches() method. this method returns boolean values if it gets same match of object value to the passes argument value in the parentheses.
Code:
/** * @(#) MatchesString.java * MatchesString class demonstrates the working of matches() method of String class of lang package * @version 16-May-2008 * @author Rose India Team */
public class MatchesString { public static void main(String args[]) {
// method just matches the String value to the specified value in the // parentheses and generates boolean values String str = " heram", str1 = "xaaaaa"; boolean result = str1.matches("aaaaa"); // method not get the same values therefore it returns false System.out.println("Method returns 'false': " + result);
// method gets the same values but not able to match there allignments // therefore it returns false result = str.matches("heram"); System.out.println("Method returns 'false': " + result);
// method gets the same values with precise allignments therefore it // returns true result = str.matches(" heram"); System.out.println("Method returns 'true': " + result); } }