Java - String matches() 方法



Java String matches() 方法用於檢查當前字串是否與給定的正則表示式匹配。正則表示式是一種模式,它指示字串只能包含數字或字母字元。任何字元集或模式都可以用於正則表示式。

例如,我們經常需要構建程式碼來確定字串是否為數字或是否包含字母;確定字串是否包含特定數字四次;字串是否僅由 A 到 Z 或 a 到 z 等字母組成等。Java 程式設計中有許多需要正則表示式的場景。

語法

以下是Java String matches()方法的語法:

public boolean matches(String regex)

引數

  • regex - 這是要與此字串匹配的正則表示式。

返回值

當且僅當此字串與給定的正則表示式匹配時,此方法返回 true。

匹配字串示例

以下示例演示了 Java String matches() 方法的使用。這裡我們建立了兩個字串 'str1' 和 'str2',其值分別為 "tutorials" 和 "learning"。然後,給定的字串與提供的正則表示式進行匹配:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String str1 = "tutorials", str2 = "learning";       
      boolean retval = str1.matches(str2);      
      
      // method gets different values therefore it returns false
      System.out.println("Value returned = " + retval); 
      retval = str2.matches("learning");      
      
      // method gets same values therefore it returns true
      System.out.println("Value returned = " + retval);    
      retval = str1.matches("tuts");      
      
      // method gets different values therefore it returns false
      System.out.println("Value returned = " + retval);
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果

Value returned = false
Value returned = true
Value returned = false

使用 Pattern 進行字串匹配示例

在下面的程式中,我們定義了一個用於數字匹配的正則表示式。當要匹配的字串僅包含數字時,返回結果 "true";當包含字母和數字;數字和字元組合時,返回 "false",因為它不匹配正則表示式。

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String RegexExpression = "^[0-9]+$";
      System.out.println("The returned value is: " + "8785".matches(RegexExpression));
      System.out.println("The returned value is: " + "@%*6575".matches(RegexExpression));
      System.out.println("The returned value is: " + "675hjh".matches(RegexExpression));
   }
}

輸出

讓我們編譯並執行上面的程式,輸出將顯示如下:

The returned value is: true
The returned value is: false
The returned value is: false

檢查字串是否僅包含字母字元示例

我們現在將建立一個程式來確定字串是否僅包含字母字元。為此,應該提供包含“A 到 Z”、“a 到 z”和一個空格 (' ') 的正則表示式,因為字串可能包含多個單詞。

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String RegexExpression = "[a-zA-Z ]+";
      String s1 = "Tutorials Point is a reputed firm";
      String s2 = "Tutori@ls && Po!nt is @ reputed f!rm";
      String s3 = "Tutorials Point is a reputed firm 766868";
      boolean Match1 = s1.matches("[a-zA-Z ]+");
      boolean Match2 = s2.matches("[a-zA-Z ]+");
      boolean Match3 = s3.matches("[a-zA-Z ]+");
      System.out.println("Is the string having only alphabets: " + Match1);
      System.out.println("Is the string having only alphabets: " + Match2);
      System.out.println("Is the string having only alphabets: " + Match3);
   }
}

輸出

在執行以上程式時,輸出如下所示

Is the string having only alphabets: true
Is the string having only alphabets: false
Is the string having only alphabets: false

獲取無效模式的 PatternSyntaxException 示例

在下面給出的程式中,正則表示式定義不正確。由於語法錯誤,引發了一個異常:

public class StringDemo {
   public static void main(String[] args) {
      String s = "Coding";
      String regexExpression = "[Cod...!!";
      
      // printing the result
      System.out.println("The returned value is: " + s.matches(regexExpression));
   }
}

執行時異常

上面程式的輸出如下所示

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 8
[Cod...!!
      ^
      at java.base/java.util.regex.Pattern.error(Pattern.java:2038)
      at java.base/java.util.regex.Pattern.clazz(Pattern.java:2700)
      at java.base/java.util.regex.Pattern.sequence(Pattern.java:2149)
      at java.base/java.util.regex.Pattern.expr(Pattern.java:2079)
      at java.base/java.util.regex.Pattern.compile(Pattern.java:1793)
      at java.base/java.util.regex.Pattern.<init>(Pattern.java:1440)
      at java.base/java.util.regex.Pattern.compile(Pattern.java:1079)
      at java.base/java.util.regex.Pattern.matches(Pattern.java:1184)
      at java.base/java.lang.String.matches(String.java:2838)
      at StringDemo.main(StringDemo.java:6)
java_lang_string.htm
廣告