正規表現の書き方を変更しました。
何が違うか、確認してみてください。
import java.util.regex.Pattern;
public class StringUtil2 {
/**
* alphabetがアルファベットの場合に、trueを返す。
*
* @param alphabet 文字列
* @return boolean true : アルファベット / false : アルファベット以外
*/
public static boolean isAlphabet(String alphabet) {
try {
return (Pattern.compile("^[a-zA-Z]*$")).matcher(alphabet).matches();
} catch (Exception e) {
return false;
}
}
/**
* characterが英数字の場合に、trueを返す。
*
* @param character 文字列
* @return boolean true : 英数字 / false : 英数字以外
*/
public static boolean isNumAlphabet(String character) {
try {
return (Pattern.compile("^[a-zA-Z0-9]*$")).matcher(character).matches();
} catch (Exception e) {
return false;
}
}
/**
* numが数字の場合に、trueを返す。
*
* @param num 文字列
* @return boolean true : 数字 / false : 数字以外
*/
public static boolean isNum(String num) {
try {
return (Pattern.compile("^[0-9]*$")).matcher(num).matches();
} catch (Exception e) {
return false;
}
}
/**
* markが半角記号の場合に、trueを返す。
*
* @param mark 文字列
* @return boolean true : 半角記号 / false : 半角記号以外
*/
public static boolean isMark(String mark) {
try {
return (Pattern.compile("^[ -/:-@\\[-\\`\\{-\\~]*$")).matcher(mark).matches();
} catch (Exception e) {
return false;
}
}
/**
* characterが半角の場合に、trueを返す。
*
* @param character 文字列
* @return boolean true : 半角 / false : 半角以外
*/
public static boolean isOneByteChar(String character) {
try {
return (Pattern.compile("^[a-zA-Z0-9 -/:-@\\[-\\`\\{-\\~]*$")).matcher(character).matches();
} catch (Exception e) {
return false;
}
}
/**
* kanaが半角カナの場合に、trueを返す。
*
* @param kana 文字列
* @return boolean true : 半角カナ / false : 半角カナ以外
*/
public static boolean isOneByteKana(String kana) {
try {
return (Pattern.compile("^[。-゚+]*$")).matcher(kana).matches();
} catch (Exception e) {
return false;
}
}
/**
* hiraganaがひらがなの場合に、trueを返す。
*
* @param hiragana 文字列
* @return boolean true : ひらがな / false : ひらがな以外
*/
public static boolean isHiragana(String hiragana) {
try {
return (Pattern.compile("^[ぁ-ゞ]*$")).matcher(hiragana).matches();
} catch (Exception e) {
return false;
}
}
/**
* katakanaがカタカナの場合に、trueを返す。
*
* @param katakana 文字列
* @return boolean true : カタカナ文字 / false : カタカナ以外
*/
public static boolean isKatakana(String katakana) {
try {
return (Pattern.compile("^[ァ-ヶ]*$")).matcher(katakana).matches();
} catch (Exception e) {
return false;
}
}
/**
* kanjiが漢字の場合に、trueを返す。
*
* @param kanji 文字列
* @return boolean true : 漢字 / false : 漢字以外
*/
public static boolean isKanji(String kanji) {
try {
return (Pattern.compile("^[一-龠]*$")).matcher(kanji).matches();
} catch (Exception e) {
return false;
}
}
/**
* zenkakuが全角の場合に、trueを返す。
*
* @param kanji 文字列
* @return boolean true : 全角 / false : 全角以外
*/
public static boolean isTwoByteChar(String zenkaku) {
try {
return (Pattern.compile("^[^ -~。-゚]*$")).matcher(zenkaku).matches();
} catch (Exception e) {
return false;
}
}
}
0 件のコメント:
コメントを投稿