English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java正規表現のサブエクスプレッション「[...]」の説明

正文 子表現「 [...]

例1

import java.util.regex.Matcher;
import java.util.regex.Pattern;
”括号内に指定された任意の単一文字を一致させる。
   public static void main( String args[] ) {
      public class SpecifiedCharacters {
      String regex = "[hwt]";3String input = "Hi how are you welcome to w"
      Pattern p = Pattern.compile(regex);
      codebox";
      Matcher m = p.matcher(input);
      while(m.find()) {
         int count = 0;++;
      }
      System.out.println("試合数: ");+件数);
   }
}

出力結果

試合数: 6

例2

以下のJavaプログラムはユーザーから5文字列を入力し、元音文字を含む文字列を表示する/単語。

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
   public static void main( String args[] ) {
      String regex = "^.*[aeiou].*$";
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter 5 入力文字列: ");
      String input[] = new String[5];
      for(int i=0; i<5; i++) {
         input[i] = sc.nextLine();
      }
      //Patternオブジェクトを生成します
      Pattern p = Pattern.compile(regex);
      System.out.println("母音文字を含む文字列: ");
      for(int i=0; i<5;i++) {
         //Matcherオブジェクトを生成します
         Matcher m = p.matcher(input[i]);
         if(m.matches()) {
            System.out.println(m.group());
         }
      }
   }
}

出力結果

Enter 5 入力文字列:
hello
sample
rhythm
cry
gym
母音文字を含む文字列:
hello
sample
基本チュートリアル