-
-
Save taichi/884610 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class RegexpSample { | |
/** 英数字の1文字以上5文字以下 */ | |
private static final String REGEX = "[0-9a-z]{1,5}"; | |
/** | |
* 正規表現を満たすかどうかの判定メソッド | |
* | |
* @param str | |
* 判定対象となる文字列 | |
* @param regex | |
* 正規表現パターン | |
* @return | |
*/ | |
private static boolean isMatchRegexp(String str, Pattern regex) { | |
Matcher m = regex.matcher(str); | |
return m.matches(); | |
} | |
/** | |
* 動作確認 | |
*/ | |
public static void main(String[] args) { | |
String s1 = "abcD"; | |
String s2 = "125Za"; | |
String s3 = "aaaaaa"; | |
String s4 = "#abc"; | |
// 正規表現のコンパイルはコストがかかるのでなるべく回数を減らす。 | |
// 大文字・小文字を区別しない | |
Pattern p = Pattern.compile(REGEX ,Pattern.CASE_INSENSITIVE); | |
System.out.println("s1 : " + isMatchRegexp(s1, p)); | |
System.out.println("s2 : " + isMatchRegexp(s2, p)); | |
System.out.println("s3 : " + isMatchRegexp(s3, p)); | |
System.out.println("s4 : " + isMatchRegexp(s4, p)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RegexpSampleCostfull { | |
/** 英数字の1文字以上5文字以下 */ | |
// 大文字・小文字を区別しない | |
private static final String REGEX = "[0-9a-zA-Z]{1,5}"; | |
/** | |
* 正規表現を満たすかどうかの判定メソッド | |
* | |
* @param str | |
* 判定対象となる文字列 | |
* @param regex | |
* 正規表現パターン | |
* @return | |
*/ | |
private static boolean isMatchRegexp(String str, String regex) { | |
// 逐次的に正規表現をコンパイルする事を許容するなら、 | |
// java.util.regexパッケージを使用する意味は無い。 | |
return str.matches(regex); | |
} | |
/** | |
* 動作確認 | |
*/ | |
public static void main(String[] args) { | |
String s1 = "abcD"; | |
String s2 = "125Za"; | |
String s3 = "aaaaaa"; | |
String s4 = "#abc"; | |
System.out.println("s1 : " + isMatchRegexp(s1, REGEX)); | |
System.out.println("s2 : " + isMatchRegexp(s2, REGEX)); | |
System.out.println("s3 : " + isMatchRegexp(s3, REGEX)); | |
System.out.println("s4 : " + isMatchRegexp(s4, REGEX)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.assertFalse; | |
import static org.junit.Assert.assertTrue; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import org.junit.Test; | |
public class RegexpSampleTest { | |
/** 英数字の1文字以上5文字以下 */ | |
private static final String REGEX = "[0-9a-z]{1,5}"; | |
/** | |
* 正規表現を満たすかどうかの判定メソッド | |
* | |
* @param str | |
* 判定対象となる文字列 | |
* @param regex | |
* 正規表現パターン | |
* @return | |
*/ | |
private static boolean isMatchRegexp(String str, Pattern regex) { | |
Matcher m = regex.matcher(str); | |
return m.matches(); | |
} | |
/** | |
* 動作確認。 | |
* | |
* @throws Exception | |
*/ | |
@Test | |
public void testIsMatchRegexp() throws Exception { | |
// 大文字・小文字を区別しない | |
Pattern p = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE); | |
// 結果がどうなると正しいのか明確に記述出来るので、 | |
// サンプルコードはJUnit等を使ってテストコードで書く方が良い。 | |
assertTrue(isMatchRegexp("abcD", p)); | |
assertTrue(isMatchRegexp("125Za", p)); | |
assertFalse(isMatchRegexp("aaaaaa", p)); | |
assertFalse(isMatchRegexp("#abc", p)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment