Java の一致

王林
リリース: 2024-08-30 16:00:27
オリジナル
686 人が閲覧しました

Java Matcher クラスは、Java Regex パッケージのクラスの 1 つです。 Java Matcher クラスとパターンは Regex パッケージの一部であり、正規表現のクラスとインターフェイスを作成する機能を提供します。正規表現の演算と文字シーケンスを見つけて、正規表現の他の文字シーケンスと比較して一致するものを見つけるのに役立ちます。一致が見つかると、MatchResult インターフェイスと呼ばれるインターフェイスが作成されます。正規表現エンジンは、文字シーケンスの照合とインターフェイスの作成という定義されたシナリオを実行するために使用されます。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

Java Matcher クラスのメソッド

以下にクラスメソッドを示します:

  • パブリックブール値一致()
  • パブリックブール値 find()
  • public boolean find(int start)
  • パブリック文字列グループ()
  • public int start() および int start(int group)
  • public int end() および int end(int group)
  • public int groupCount()
  • public boolean lookingAt()
  • public Matcher appendReplacement()
  • public StringBuffer appendTail()
  • public String replaceAll()
  • public String replaceFirst()
  • public static String quoteReplacement()

1. public booleanmatchs()

booleanmatches() は、正規表現がパターンと一致するかどうかをテストするために使用される Java Matcher クラス内のメソッドです。

:

このプログラムは、パターンを使用して Java Matcher クラスの public booleanmatchs() メソッドを示します。

コード:

import java.util.regex.*;
public class BooleanRegMatches{
public static void main(String args[])
{
System.out.println(Pattern.matches("[banana]", "qda"));
System.out.println(Pattern.matches("[mango]", "mn"));
System.out.println(Pattern.matches("[orange]", "e"));
}
}
ログイン後にコピー

出力:

Java の一致

2. public boolean find()

このメソッドはマッチャー クラスの一部であり、次の式を検索して照合するために使用されます。この式はパターンと一致し、インターフェイスの作成に使用できます。

例:

このプログラムは、正規表現を検索して照合し、パターンと照合および比較してインターフェイスを作成するために使用されます。

コード:

import java.util.regex.Pattern;
import java.util.Scanner;
import java.util.regex.Matcher;
public class Regx_Pattrn_1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while (true) {
System.out.println("consider a regulr exprssn for traversing:");
Pattern pattern_k = Pattern.compile(sc.nextLine());
System.out.println("Next Text to Execute:");
Matcher matcher_g = pattern_k.matcher(sc.nextLine());
boolean found = false;
while (matcher_g.find()) {
System.out.println(" Text needs to be searched  "+matcher_g.group()+" from starting index "+
matcher_g.start()+" and from ending index "+matcher_g.end());
found = true;
}
if(!found){
System.out.println("No match is found after comparison");
}
}
}
}
ログイン後にコピー

出力:

Java の一致

3. public boolean find(int start)

このメソッドは、先頭または末尾のインデックスから始まる正規表現に一致するメソッドを見つけるために使用されます。

例:

このプログラムは、正規表現の開始インデックスと終了インデックスを与えるブール型 find(int start) メソッドを示します。

コード:

import java.util.regex.Pattern;
import java.util.Scanner;
import java.util.regex.Matcher;
public class Java_Pattrn_2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while (true) {
System.out.println("Form one reg_expression pattern:");
Pattern pattern_j = Pattern.compile(sc.nextLine());
System.out.println("Neeed to match some part of the string for execution:");
Matcher matcher_o = pattern_j.matcher(sc.nextLine());
boolean found1 = false;
while (matcher_o.find()) {
System.out.println("got the pattern initiated where "+matcher_o.group()+" index_is_starting "+
matcher_o.start()+" index_is_ending "+matcher_o.end());
found1 = true;
}
if(!found1){
System.out.println("Some kind of mismatch is prevailing");
}
}
}
}
ログイン後にコピー

出力:

Java の一致

4. public String group()

このメソッドは、正規表現の一致した文字列を後で返す Java Matcher クラスの一部です。

例:

このプログラムは、Java Matcher クラスの String group () メソッドを示します。このメソッドは、一致文字列にヒットした後、正規表現の後続の部分文字列を生成しようとします。

コード:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Regx_Pttrn_3 {
public static void main(String[] args) {
String reg_exprn_txt ="I am trying to learn something new, and trying to learning something from there," +
" and will one day become a skilled person. ";
String pattern_one_str = "(trying)";
Pattern pattrn_7 = Pattern.compile(pattern_one_str);
Matcher matchr_0 = pattrn_7.matcher(reg_exprn_txt);
while(matchr_0.find()) {
System.out.println(" Matched_Value: " + matchr_0.group(1));
}
}
}
ログイン後にコピー

出力:

Java の一致

5. public int start() および int start(int group)

Java Matcher クラスの一部である int start () および int start(int group) メソッドは、グループ化された文字列の開始インデックスを持つ文字列の部分文字列の後続の値を取得するために使用されます。

:

このプログラムは、文字列のグループ化に続いて、正規表現の開始インデックスに一致する後続のマッチャー文字列を見つける方法を示します。

コード:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Regx_Pttrn4 {
public static void main(String[] args) {
String reg_txt_exprssion    =
"This text needs to be done " +
"with some careful attention 'done'.";
String string_grouping = "done";
Pattern pattern_l = Pattern.compile(string_grouping);
Matcher matcher_l = pattern_l.matcher(reg_txt_exprssion);
int count = 0;
while(matcher_l.find()) {
count++;
System.out.println("Matching_context: " + count + " : "
+ matcher_l.start());
}
}
}
ログイン後にコピー

出力:

Java の一致

6. public int end() と int end(in group)

このメソッドは、一致した後続の文字列パターンの部分文字列の終了インデックスを返すために使用されます。

例:

このプログラムは、Java Matcher クラスの int end (グループ内) メソッドをデモンストレーションするために使用されます。

コード:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Regx_Pattrn5 {
public static void main(String[] args) {
String Expression    =
"Some special substring characters and escape characters need to be searched for. " +
"that to get the number of hits on the given expression of the text 'to'.";
String string_Pattrn = "to";
Pattern pattern_f = Pattern.compile(string_Pattrn);
Matcher matcher_f = pattern_f.matcher(Expression);
int count = 0;
while(matcher_f.find()) {
count++;
System.out.println("match_found: " + count + " : "
+ matcher_f.end());
}
}
}
ログイン後にコピー

出力:

Java の一致

7. public int groupCount

このメソッドは、正規表現の一致した部分文字列を比較した後、正規表現のセットとして存在するグループの数を返すために使用されます。

例:

このプログラムは、Java Matcher クラスの int group count メソッドを示します。

コード:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Regx_Pattrn_6 {
public static void main(String[] args) {
String run_Exp  = " I am fond of eating delicious and yummy food everythime, and my mom is also fond of cooking that," +
" and dad also wants to taste the yummy food.";
String patternString1 = "(fond) (.+?) ";
Pattern pattern_s = Pattern.compile(patternString1);
Matcher matcher_s = pattern_s.matcher(run_Exp);
while(matcher_s.find()) {
System.out.println("matching_found_present: " +  "count:"+ matcher_s.group(1) +
" "       + matcher_s.group(2));
}
}
}
ログイン後にコピー

出力:

Java の一致

8. public boolean lookingat()

As part of the matcher class, this method only tries to match the beginning of the text, not the entire text, which means, unlike the matcher class, which considers the entire regular expression for matching and result.

Example:

This program demonstrates the boolean lookingat() method of the Java Matcher class.

Code:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Regx_Pattrn_7 {
public static void main(String[] args) {
String String_expr = "An expression to search and match the string pattern " + "make a search for the pattern.";
String String_ptrn = "pattern";
Pattern pattern_k = Pattern.compile(String_ptrn, Pattern.CASE_INSENSITIVE);
Matcher matcher_k = pattern_k.matcher(String_expr);
System.out.println("currently_looking_At = " + matcher_k.lookingAt());
System.out.println("currently_matches   = " + matcher_k.matches());
}
}
ログイン後にコピー

Output:

Java の一致

9. public Matcher appendReplacement()

This method is used to replace the set of text in the regular expression and then append that input string into the string buffer. It keeps a check on the string buffer what is being copied and what is needed to be replaced.

Example:

This program demonstrates the Matcher appendReplacement() method of Java Matcher.

Code:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Regex_Pttrn_8 {
public static void main(String[] args) {
String sm_txt  =  "I love to read books of harry potter, and i also love to read books of JK Rowling" +   " and My favourite writer writes all fiction books.";
String String_ptrn1 = "((books) (.+?)) ";
Pattern pattern_o = Pattern.compile(String_ptrn1);
Matcher matcher_o = pattern_o.matcher(sm_txt);
StringBuffer stringBuffer = new StringBuffer();
while(matcher_o.find()){
matcher_o.appendReplacement(stringBuffer, " fiction books ");
System.out.println(stringBuffer.toString());
}
}
}
ログイン後にコピー

Output:

Java の一致

10. public StringBuffer appendTail()

This method also behaves in a similar fashion with a difference that if the last match is found from the input text and is not yet copied to the string buffer, then it is needed to check for the required text and can append at the end of the expression and then can be copied into the buffer.

Example:

This program demonstrates the StringBuffer append Tail() method of Java Matcher.

Code:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Regex_Pttrn_8 {
public static void main(String[] args) {
String sm_txt  =  "I love to read books of harry potter, and i also love to read books of JK Rowling" +  " and My favourite writer writes all fiction books.";
String String_ptrn1 = "((books) (.+?)) ";
Pattern pattern_p = Pattern.compile(String_ptrn1);
Matcher matcher_p = pattern_p.matcher(sm_txt);
StringBuffer stringBuffer = new StringBuffer();
while(matcher_p.find()){
matcher_p.appendReplacement(stringBuffer, " fiction books ");
System.out.println(stringBuffer.toString());
}
matcher_p.appendTail(stringBuffer);
System.out.println(stringBuffer.toString());
}
}
ログイン後にコピー

Output:

Java の一致

11. public String replaceAll()

As its name suggests, the String replaceAll () method can replace all the found matches and matched groups with the regular expression.

Example:

This program demonstrates the String replaceAll() method of Java Matcher.

Code:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Java_Reg_Replace {
public static void main(String[] args) {
String one_ptrn    ="I want to have a cup of tea, and I also want to have a cup of coffe at the same time," +
" and also want to have toffee.";
String ptrn_ex_strng = "((cofee) (.+?)) ";
Pattern pattern1 = Pattern.compile(ptrn_ex_strng);
Matcher matcherb = pattern1.matcher(one_ptrn);
String replaceAll = matcherb.replaceAll("tea or coffee ");
System.out.println("replaceAll   = " + replaceAll);
}
}
ログイン後にコピー

Output:

Java の一致

12. public String replaceFirst()

This method is used for replacing the string text from the beginning of the regular expression as soon as the first match gets hit.

Example:

This program demonstrates the replaceFirst() method of Java Matcher.

Code:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Java_Reg_Replace {
public static void main(String[] args) {
String one_ptrn    ="I want to have a cup of tea, and I also want to have a cup of coffe at the same time," + " and also want to have toffee.";
String ptrn_ex_strng = "((cofee) (.+?)) ";
Pattern pattern1 = Pattern.compile(ptrn_ex_strng);
Matcher matcherb = pattern1.matcher(one_ptrn);
String replaceAll = matcherb.replaceAll("tea or coffee ");
System.out.println("replaceAll   = " + replaceAll);
String replaceFirst = matcherb.replaceFirst("tea or coffee ");
System.out.println("replaceFirst = " + replaceFirst);
}
}
ログイン後にコピー

Output:

Java の一致

13. public static String quoteReplacement()

It is a mediator method of all the replace methods and is used when the string and the characters in the string need to be replaced with the parameters passed as a replaced parameter.

Example:

This program demonstrates the String quoteReplacement() method of Java Matcher.

Code:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class reg_quote_replcment {
public static void main(String[] args) {
String reg_exprsn = "Welcome Everyone";
Pattern pattern_a = Pattern.compile(reg_exprsn);
String stringNeedsToMatch  = "Welcome evryone to the laerning portal of educba";
Matcher matcher_b = pattern_a.matcher(stringNeedsToMatch);
String stringToBeReplaced = "Welcome";
System.out.println(matcher_b .quoteReplacement(stringToBeReplaced));
}
}
ログイン後にコピー

Output:

Java の一致

Conclusion

Java Matcher class is a class which is part of Java regex package and mainly deals with the regular expressions and its functioning with the other regular function at the index level, replacement methods and quotereplacement method. It has simplified the overall manipulation of the strings and characters of the string for regular expressions.

以上がJava の一致の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!