English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
このjava.util.regex.Matcherのこのクラスはエンジンを表し、さまざまなマッチング操作を行います。このクラスにはコンストラクタがありませんが、matches()
java.util.regex.Patternのメソッドで作成/このクラスのオブジェクトを取得します。
MatcherクラスのtoString()このメソッドは、現在のマッチャーオブジェクトの内容を表す文字列値を返します。
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ToStringExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); String regex = "[#%&*]"; //パターンオブジェクトを作成します Pattern pattern = Pattern.compile(regex); //Matcher オブジェクトを生成します Matcher matcher = pattern.matcher(input); int count =0; while(matcher.find()) { count++; } //検索に使用されるパターン System.out.println("以下は、"+count+" 特殊文字 [# % & *] テキスト中の文字数 characters in the given text"); System.out.println("以下は、マッチャーに使用される文字列形式です:\n"+matcher.toString()); } }
出力結果
入力テキストを入力してください: Hello# How # are# you *& welcome to T#utorials%point 以下は、 7 特殊文字 [# % & *] テキスト中の文字数 characters in the given text 以下は、マッチャーに使用される文字列形式です: java.util.regex.Matcher[pattern=[#%&*] region=0,52 lastmatch=]
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ToStringExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); String regex = "[#%&*]"; //パターンオブジェクトを作成します Pattern pattern = Pattern.compile(regex); //Matcher オブジェクトを生成します Matcher matcher = pattern.matcher(input); int count =0; while(matcher.find()) { count++; } //検索に使用されるパターン System.out.println("以下は、"+count+" 特殊文字 [# % & *] テキスト中の文字数 characters in the given text"); System.out.println("以下は、マッチャーに使用される文字列形式です:\n"+matcher.toString()); } }
出力結果
入力テキストを入力してください: Hello# How # are# you *& welcome to T#utorials%point 以下は、 7 特殊文字 [# % & *] テキスト中の文字数 characters in the given text 以下は、マッチャーに使用される文字列形式です: java.util.regex.Matcher[pattern=[#%&*] region=0,52 lastmatch=]