最短一致再びです。
2013/10/29
java
正規表現
最短一致再びです。
cssファイルのブラケットの中身をなんとかしたいと思いはて、正規表現でどうするのだろうと思ったのでした。忘れてる。
プログラマメモ2: せいき表現練習 - 最短一致
完全に忘れてました。。。。
で、cssのブラケットの中身とブラケットを消して、セレクターの部分をとりだしたいので、とりあえずブラケットとブラケットの中身を消します。
package regex;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestCssBrReplacer {
public static byte[] getBytes(InputStream in) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
int len = 0;
byte[] bs = new byte[8192];
while ((len = in.read(bs)) != -1) {
outputStream.write(bs, 0, len);
outputStream.flush();
}
outputStream.close();
} finally {
in.close();
}
return outputStream.toByteArray();
}
public static void main(String[] args) throws IOException {
TestCssBrReplacer t = new TestCssBrReplacer();
String s = new String(getBytes(t.getClass().getResourceAsStream(
"doc-files/a.css")));
t.a(s);
}
void a(String s) {
String s1 = "";
Pattern pattern = Pattern.compile("\\{.*?\\}", Pattern.DOTALL);
Matcher matcher = pattern.matcher(s);
s1 = matcher.replaceAll("");
System.out.println(s1);
}
}
おまけ
package regex;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestCssBr {
public static byte[] getBytes(InputStream in) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
int len = 0;
byte[] bs = new byte[8192];
while ((len = in.read(bs)) != -1) {
outputStream.write(bs, 0, len);
outputStream.flush();
}
outputStream.close();
} finally {
in.close();
}
return outputStream.toByteArray();
}
public static void main(String[] args) throws IOException {
TestCssBr t = new TestCssBr();
String s = new String(getBytes(t.getClass().getResourceAsStream(
"doc-files/a.css")));
t.a(s);
}
void a(String s) {
Pattern pattern = Pattern.compile("\\{.*?\\}",
Pattern.DOTALL);
Matcher matcher = pattern.matcher(s);
int cnt = 0;
while (matcher.find()) {
System.out.println("*** " + ++cnt);
System.out.println(matcher.group(0));
}
}
}
: