グループ数のカウントJava正規表現
複数のキャラクターをグループとしてキャプチャすることで、複数のキャラクターを1つのユニットとして扱うことができます。これらの文字を括弧のセットの中に配置する必要があります。
groupCount()を使用して、現在の試合のグループ数を数えることができます Matcherクラスのメソッド。このメソッドは、現在の一致のキャプチャグループの数を計算し、それを返します。
例
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String str1 = "<p>This <b>is</b> an <b>example</b> HTML <b>script</b> where <b>ever</b> alternative <b>word</b> is <b>bold</b></p>.";
//Regular expression to match contents of the bold tags
String regex = "(t(\\S+)t)(\\s)";
String str = "the words tit tat tweet tostff tact that tilt text. start and end with the letter t ";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
System.out.println("Total capturing groups: "+matcher.groupCount());
}
} 出力
tit tat tweet tact that tilt text tart Total capturing groups: 3
-
Javaでの正規表現(再)サブ式
部分式/メタ文字「()」は正規表現をグループ化し、一致したテキストを記憶します。 例1 import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main( String args[] ) { String input = "Hello how are you welcome to Tutorialspoint&q
-
Python正規表現でキャプチャグループの数を取得するにはどうすればよいですか?
次のコードは、指定された文字列でPython正規表現を使用してキャプチャされたグループの数を取得します 例 import re m = re.match(r"(\d)(\d)(\d)", "632") print len(m.groups()) 出力 これにより、出力が得られます 3