文字列からの情報抽出
Posted feedbacks - Java
やはり一番乗りは無理ですね…… 出力結果を示しておきます。 name:'abc', ext:'png', size: normal, hidden true name:'hoge', ext:'jpeg', size: big, hidden false name:'foo', ext:'gif', size: small, hidden true name:'a', ext:'bmp', size: normal, hidden false name:'hiddena', ext:'png', size: normal, hidden false name:'small', ext:'jpg', size: normal, hidden false name:'normal', ext:'hoge', size: big, hidden false #5行目がサンプル出力と異なるのはサンプル入力の問題と思われます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import java.util.regex.*;
import java.util.*;
public class Sample {
private static final Pattern FILE_NAME_PATTERN = Pattern.
compile("¥¥b(¥¥p{Alpha}+)((¥¥-¥¥p{Alpha}+)*)¥¥.(¥¥p{Alpha}+)¥¥b");
public static void filter(String in) {
Matcher m = FILE_NAME_PATTERN.matcher(in);
while (m.find()) {
Set<String> attrSet= new HashSet<String>(Arrays.asList(m.group(2).
split("¥¥-")));
String size = "normal";
if (attrSet.contains("big")) {
size = "big";
} else if (attrSet.contains("small")) {
size = "small";
}
System.out.printf("name:'%s', ext:'%s', size: %s, hidden %b%n",
m.group(1), m.group(4), size, attrSet.contains("hidden"));
}
}
public static void main(String[] args) {
filter("aaa abc-hidden.png>hoge-big.jpeg¥n" +
"---foo-hidden-small.gif|^_^a.bmp¥n" +
"--hiddena-hoge.png<=not hidden‾‾¥n" +
"--small.jpg<=not small(^_^)¥n" +
"normal-small-big.hoge¥n");
}
}
|
その通りですね。訂正しました(今度こそ大丈夫でしょうか)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import java.util.regex.*;
public class Sample {
private static final Pattern FILE_NAME_PATTERN = Pattern.compile(
"¥¥b(¥¥p{Alpha}+)(-hidden)?(-small|-big)?¥¥.(¥¥p{Alpha}+)¥¥b");
public static void filter(String in) {
Matcher m = FILE_NAME_PATTERN.matcher(in);
while (m.find()) {
String size = "normal";
if ("-big".equals(m.group(3))) {
size = "big";
} else if ("-small".equals(m.group(3))) {
size = "small";
}
System.out.printf("name:'%s', ext:'%s', size: %s, hidden %b%n",
m.group(1), m.group(4), size, "-hidden".equals(m.group(2)));
}
}
public static void main(String[] args) {
filter("aaa abc-hidden.png>hoge-big.jpeg¥n" +
"---foo-hidden-small.gif|^_^a.bmp¥n" +
"--hiddena-hoge.png<=not hidden‾‾¥n" +
"--small.jpg<=not small(^_^)¥n" +
"normal-small-big.hoge¥n");
}
}
|




にしお
#3407()
Rating0/0=0.00
サンプル入力
サンプル出力
探すべき文字列は下の条件を満たします
出力は以下の条件を満たす必要があります
このお題は、正規表現のグループに名前をつけて連想配列として取得できるPythonからの挑戦状です。
[ reply ]