Skip to content

Instantly share code, notes, and snippets.

@openbooks
Last active July 15, 2016 16:02
Show Gist options
  • Save openbooks/d008b25d6240b67d3123 to your computer and use it in GitHub Desktop.
Save openbooks/d008b25d6240b67d3123 to your computer and use it in GitHub Desktop.
/**
* Copyright 2014 by openbooks, All rights reserved.
*/
package com.openbooks.sample.marches;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* このクラスは、正規表現を確認するために用意したクラスです。
*
* @author openbooks
* @version 0.0.1, 2014/11/16
* @since 0.0.1
*/
public class ExampleMatches {
/**
* 正規表現のパターン郡列挙オブジェクト
*/
public enum PatternEnum {
/** 英数字のみ: ^[a-zA-Z0-9]+$ */
ALNUM("^[a-zA-Z0-9]+$"),
/** 英字のみ: ^[a-zA-Z]+$ */
ALPHA("^[a-zA-Z]+$"),
/** すべての ASCII 文字: [\x00-\x7F] */
ASCII("[\\x00-\\x7F]"),
/** 10 進数字のみ: ^[0-9]+$ */
DIGIT("^[0-9]+$"),
/** 小文字の英字のみ:[a-z] */
LOWER("^[a-z]+$"),
/** 郵便番号(半角数値3桁 半角ハイフン 半角数値4桁): ^\\d{3}-\\d{4}$*/
POSTALCODE("^\\d{3}-\\d{4}"),
/** 句読文字:!"#$%&'()*+,-./:;<=>?@[]^_`{ */
PUNCT("!\"#$%&'()*+,-./:;<=>?@[]^_`{]+$"),
/** 空白文字:[ \t\n\x0B\f\r] */
SPACE(" \\t\\n\\x0B\\f\\r"),
/** 大文字の英字のみ:^[A-Z]+$ */
UPPER("^[A-Z]+$"),
/** 16 進数字のみ:[0-9a-fA-F] */
XDIGIT("^[0-9a-fA-F]+$");
/** 正規表現 */
private final String regex;
private PatternEnum(final String regex) {
this.regex = regex;
}
/** 正規表現を保持する{@code Pattern}オブジェクトを返却する */
@SuppressWarnings("unqualified-field-access")
public Pattern toPattern() {
return Pattern.compile(regex);
}
}
/**
* このメソッドは、patternオブジェクトに指定された正規表現に属している場合はtrueを返却し属していない場合はfalseを返却する。<br>
* @param pattern 正規表現を保持するオブジェクト
* @param input 正規表現を利用して確認したい対象オブジェクト
* @return {@link Matcher#find()}の結果を返却する
*/
public static boolean findMatches(Pattern pattern, CharSequence input) {
final Matcher m = pattern.matcher(input);
return m.find();
}
/**
* このメソッドは、内部で{@code ExampleMatches#findMatches(Pattern, CharSequence)}を呼び出しています。
* @param patternEnum 正規表現を保持するオブジェクト
* @param input 正規表現を利用して確認したい対象オブジェクト
* @return {@link ExampleMatches#findMatches(Pattern, CharSequence)}のjavadocを参照
*/
public static boolean findMatches(PatternEnum patternEnum, CharSequence input) {
return findMatches(patternEnum.toPattern(), input);
}
}
package com.openbooks.sample.marches;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.regex.Pattern;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.openbooks.sample.marches.ExampleMatches.PatternEnum;
public class ExampleMatchesTest {
@BeforeClass
public static void setUpBeforeClass() {
}
@Before
public void setUp() {
}
// PatternEnum.DIGITのテスト ここから
@SuppressWarnings("boxing")
@Test(timeout=100)
public void 正規表現で数字のみの場合に数字と数字以外を渡すとfalseが返却されること() {
final Pattern pattern = PatternEnum.DIGIT.toPattern(); // 数字のみ
final boolean result = ExampleMatches.findMatches(pattern, "0一2三4五6七8九");
assertThat(result, is(Boolean.FALSE));
}
@SuppressWarnings("boxing")
@Test(timeout=100)
public void 正規表現で数字のみの場合に数字を渡すとtrueが返却されること() {
final Pattern pattern = PatternEnum.DIGIT.toPattern(); // 数字のみ
final boolean result = ExampleMatches.findMatches(pattern, "0123456789");
assertThat(result, is(Boolean.TRUE));
}
@SuppressWarnings("boxing")
@Test(timeout=100)
public void 引数にPatternEnumを渡すパターン_正規表現で数字のみの場合に数字を渡すとtrueが返却されること() {
final boolean result = ExampleMatches.findMatches(PatternEnum.DIGIT, "0123456789");
assertThat(result, is(Boolean.TRUE));
}
@SuppressWarnings("boxing")
@Test(timeout=100)
public void 正規表現で数字のみの場合に数字以外を渡すとfalseが返却されること() {
final Pattern pattern = PatternEnum.DIGIT.toPattern(); // 数字のみ
final boolean result = ExampleMatches.findMatches(pattern, "一二三四五六七八九零");
assertThat(result, is(Boolean.FALSE));
}
@SuppressWarnings("boxing")
@Test(timeout=100)
public void 正規表現で数字のみの場合に全角の数字を渡すとfalseが返却されること() {
final Pattern pattern = PatternEnum.DIGIT.toPattern(); // 数字のみ
final boolean result = ExampleMatches.findMatches(pattern, "0123456789");
assertThat(result, is(Boolean.FALSE));
}
}
// PatternEnum.DIGITのテスト ここまで
public static void main(String args[]) {
//判定する文字列
String str = "123A5";
//判定するパターンを生成
Pattern p = Pattern.compile("^[0-9]*$");
Matcher m = p.matcher(str);
//画面表示
System.out.println(m.find());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment