English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
本文/サブエクスプレッションメタ文字「^」
import java.util.regex.Matcher; import java.util.regex.Pattern; 行の先頭に一致します。正規表現にこのコマンドを使用すると、入力文字列のその後の行と一致します。 public static void main(String args[]) { public class RegexExample { String regex = "^Hi how are you"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("試合数: ")+; } }
出力結果
試合数: 1
以下のJavaプログラムはユーザーから5文字列を入力し、数字で始まる文字列を出力します。
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class StartingwithDigit { public static void main(String args[]) { String regex = "^[0-9].*$"; 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()); } } } }
出力結果
入力 5 入力文字列: sample string 1 sample string 2 11 sample string 3 22 sample string 4 43534 56353 636 数字で始まる文字列: 11 sample string 3 22 sample string 4 43534 56353 636