回答編集履歴

3

コメントおよび修正

2017/07/19 14:55

投稿

swordone
swordone

スコア20651

test CHANGED
@@ -1,14 +1,8 @@
1
1
  [MatcherクラスのgroupCountメソッド](https://docs.oracle.com/javase/jp/8/docs/api/java/util/regex/Matcher.html#groupCount--)で出来ませんか?
2
-
3
-
4
2
 
5
3
  ###実験コード
6
4
 
7
5
  ```java
8
-
9
- package teratail;
10
-
11
-
12
6
 
13
7
  import java.util.regex.Pattern;
14
8
 
@@ -26,7 +20,7 @@
26
20
 
27
21
  .map(Pattern::compile)
28
22
 
29
- .map(x -> x.matcher(""))
23
+ .map(x -> x.matcher("")) // Matcherを作成することが目的なので、渡す文字列はnullでなければ何でもいい
30
24
 
31
25
  .map(x -> x.pattern() + " : " + x.groupCount() + "グループ")
32
26
 

2

実験

2017/07/19 14:55

投稿

swordone
swordone

スコア20651

test CHANGED
@@ -1 +1,53 @@
1
1
  [MatcherクラスのgroupCountメソッド](https://docs.oracle.com/javase/jp/8/docs/api/java/util/regex/Matcher.html#groupCount--)で出来ませんか?
2
+
3
+
4
+
5
+ ###実験コード
6
+
7
+ ```java
8
+
9
+ package teratail;
10
+
11
+
12
+
13
+ import java.util.regex.Pattern;
14
+
15
+ import java.util.stream.Stream;
16
+
17
+
18
+
19
+ public class Q84924 {
20
+
21
+
22
+
23
+ public static void main(String[] args) {
24
+
25
+ Stream.of("(.+)_(\\d{4,8})", "(.+)_(\\d{4,8})_(\\S\\(\\S\\))", "\\(abc\\)")
26
+
27
+ .map(Pattern::compile)
28
+
29
+ .map(x -> x.matcher(""))
30
+
31
+ .map(x -> x.pattern() + " : " + x.groupCount() + "グループ")
32
+
33
+ .forEach(System.out::println);
34
+
35
+ }
36
+
37
+
38
+
39
+ }
40
+
41
+
42
+
43
+ ```
44
+
45
+ ###結果
46
+
47
+ (.+)_(\d{4,8}) : 2グループ
48
+
49
+ (.+)_(\d{4,8})_(\S\(\S\)) : 3グループ
50
+
51
+ \(abc\) : 0グループ
52
+
53
+

1

リンク

2017/07/19 14:49

投稿

swordone
swordone

スコア20651

test CHANGED
@@ -1 +1 @@
1
- [MatcherクラスのgroupCountメソッド]()で出来ませんか?
1
+ [MatcherクラスのgroupCountメソッド](https://docs.oracle.com/javase/jp/8/docs/api/java/util/regex/Matcher.html#groupCount--)で出来ませんか?