Skip to content

Instantly share code, notes, and snippets.

@elnikkis
Created July 21, 2017 04:33
Show Gist options
  • Save elnikkis/d86dc4c7fa180206e6a2f2585d3a8a92 to your computer and use it in GitHub Desktop.
Save elnikkis/d86dc4c7fa180206e6a2f2585d3a8a92 to your computer and use it in GitHub Desktop.
import java.util.regex.*;
/*
* 文字列定数を受理する正規表現
*/
class StringRegex
{
String[] testcase = {
"\"This is a string.\"",
"\"This is a \"string\".\"",
"\"This is a \\\"string\\\".\"",
"\"This is a string.\\\"",
"\"This is a \\\\ string.\""
};
boolean[] isAccept = { true, false, true, false };
String regex = "\"(([^\\\\\"])|(\\\\.))*\"";
public StringRegex()
{
System.out.printf("pattern: %s\n", regex);
//print();
Pattern p = Pattern.compile(regex);
for(int i=0; i<testcase.length; i++){
Matcher m = p.matcher(testcase[i]);
// 全体
boolean res = m.matches();
System.out.printf("%s , %s\n", testcase[i], String.valueOf(res));
// 部分
m.reset();
while(m.find()){
System.out.println(m.group());
}
}
}
void print()
{
// print testcase
for(int i=0; i<testcase.length; i++){
System.out.printf("%s , %s\n", testcase[i], String.valueOf(isAccept[i]));
}
}
public static void main(String[] args)
{
new StringRegex();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment