Skip to content

Instantly share code, notes, and snippets.

@becurt
Forked from skRyo/gist:3859937
Created October 10, 2012 11:14
Show Gist options
  • Save becurt/3864855 to your computer and use it in GitHub Desktop.
Save becurt/3864855 to your computer and use it in GitHub Desktop.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 行の性質をパターンで分析する
* ただし、ブロックコメントの途中行については無視しここではハンドリングしない。
* 一行にブロックコメントが複数ある場合については対応しない。そんなの馬鹿げてる。
* @author becurt
*
*/
public class LinePattern implements AutoCloseable{
private static String singleComment = "^\\s*\\/\\/";
private static String singleCommentExist = "\\/\\/";
private static String blockCommentStart = "^\\s*\\/\\*";
private static String blockCommentStartExist = "\\/\\*";
private static String blockCommentEndExist = "\\*\\/";
private static String blockCommentEnd = "\\*\\/\\s*$";
private static String spaceLine = "^\\s*\\s*$";
private static Pattern singleCommentExistPattern = Pattern.compile(singleCommentExist);
private static Pattern singleCommentPattern = Pattern.compile(singleComment);
private static Pattern blockCommentStartExistPattern = Pattern.compile(blockCommentStartExist);
private static Pattern blockCommentEndExistPattern = Pattern.compile(blockCommentEndExist);
private static Pattern blockCommentStartPattern = Pattern.compile(blockCommentStart);
private static Pattern blockCommentEndPattern = Pattern.compile(blockCommentEnd);
private static Pattern spacePattern = Pattern.compile(spaceLine);
/**
* 空白行かどうかを返す
* @param line
* @return
*/
public boolean spaceLineFlg(String line){
Matcher m = spacePattern.matcher(line);
return m.find();
}
/**
* 一行コメントが含まれているかどうかを返す
* @param line
* @return
*/
public boolean singleCommentExistFlg(String line){
Matcher m1 = singleCommentExistPattern.matcher(line);
return m1.find();
}
/**
* ブロックコメントの開始記述が含まれているかどうかを返す
* @param line
* @return
*/
public boolean blockCommentStartExistFlg(String line){
Matcher m1 = blockCommentStartExistPattern.matcher(line);
return m1.find();
}
/**
* コメントの終了記述が含まれているかどうかを返す
* @param line
* @return
*/
public boolean commentEndExistFlg(String line){
Matcher m = blockCommentEndExistPattern.matcher(line);
return m.find() ;
}
/**
* 1行コメントが存在しているかどうかを返す
*/
public boolean singleCommentFlg(String line){
Matcher m = singleCommentPattern.matcher(line);
return m.find();
}
/**
* コメント開始記述および終了記述があった時にコメントとしてカウントするかどうかを返す。
* @param line
* @return
*/
public boolean commentLineStatus(String line){
Matcher m = singleCommentPattern.matcher(line);
Matcher m1 = singleCommentExistPattern.matcher(line);
Matcher m2 = blockCommentStartPattern.matcher(line);
Matcher m3 = blockCommentEndPattern.matcher(line);
Matcher m4 = blockCommentEndExistPattern.matcher(line);
if(m.find())
return true;
if(m1.find())
return false;
if(m2.find()){
if(m3.find())
return true;
if(m4.find())
return false;
return true;
}
if(m4.find()){
if(m3.find())
return true;
}
return false;
}
@Override
public void close() throws Exception {
System.out.println("お答えします。");
}
}
@becurt
Copy link
Author

becurt commented Oct 12, 2012

マッチャー間違ってたっちゃー

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment