정규표현식이 필요한 경우가 생기는데 그때 를 위해  예제를 작성해놓고 참고하자.

  • 문자 1개
  • 포함된 문자
  • 포함된 문자 조합
  • 한번에 다 써보기

 

문자 1개

Regular Expression Decription example
. 문자 1개 "h".matches(".");
String text = "a"; // 문자 1개
System.out.println("a.matches(\".\") = " + text.matches("."));
System.out.println("a.matches(\"..\") = " + text.matches(".."));
output:
a.matches(".") = true
a.matches("..") = false

text = "aa"; // 문자 2개로 변경
System.out.println("a.matches(\".\") = " + text.matches("."));
System.out.println("a.matches(\"..\") = " + text.matches(".."));
output:
aa.matches(".") = false
aa.matches("..") = true

text = "*1"; // 특수문자와 숫자로 변경
System.out.println("*1.matches(\".\") = " + text.matches("."));
System.out.println("*1.matches(\"..\") = " + text.matches(".."));
output:
*1.matches(".") = false
*1.matches("..") = true

text = "한글"; // 한글로 변경
System.out.println("한글.matches(\".\") = " + text.matches("."));
System.out.println("한글.matches(\"..\") = " + text.matches(".."));
System.out.println("한글.matches(\".+\") = " + text.matches(".+"));
System.out.println("한글.matches(\".*\") = " + text.matches(".*"));
output:
한글.matches(".") = false
한글.matches("..") = true
한글.matches(".+") = true
한글.matches(".*") = true

text = ""; // 빈 문자열로 변경
System.out.println("\"\".matches(\".+\") = " + text.matches(".+"));
System.out.println("\"\".matches(\".*\") = " + text.matches(".*"));
output:
"".matches(".+") = false // 1개 이상이라서 false
"".matches(".*") = true // 0개 이상이라서 true

text = "강아지";
System.out.println("강아지.matches(\"강아.\") = " + text.matches("강아."));
System.out.println("강아지.matches(\"강아지.\") = " + text.matches("강아지."));
output:
강아지.matches("강아.") = true
강아지.matches("강아지.") = false // 강아지 뒤에 문자가 없기 때문에 false

text = "김철수";
System.out.println("김철수.matches(\"김.수\") = " + text.matches("김.수"));
output:
김철수.matches("김.수") = true

text = "김박수";
System.out.println("김박수.matches(\"김.수\") = " + text.matches("김.수"));
output:
김박수.matches("김.수") = true
  • 표현식 한개당 문자 1개를 의미
  • 표현식을 두개 사용하면 문자 2개를 의미한다.
  • 특수문자, 숫자, 한글 모두 가능하다.
  • 표현식 뒤에 '+' 를 붙이면 '문자 1개 이상' 을 의미한다.
  • 표현식 뒤에 '*' 를 붙이면 '문자 0개 이상' 을 의미한다.
  • 정해진 문자가 있고 특정 위치에 임의의 문자가 필요한 경우 사용하면 좋을듯하다.

정규표현식에 포함된 문자 1개

Regular Expression Decription example
[expression] expression에 포함된 문자열 중에 하나라도 존재하면 true "a".matches("[abc]")
String text = "a";
System.out.println("a.matches(\"[abc]\") = " + text.matches("[abc]"));
System.out.println("a.matches(\"[bcd]\") = " + text.matches("[bcd]"));
output:
a.matches("[abc]") = true
a.matches("[bcd]") = false

text = "abc";
System.out.println("abc.matches(\"[abc]\") = " + text.matches("[abc]"));
System.out.println("abc.matches(\"[abcd]\") = " + text.matches("[abcd]"));
output:
abc.matches("[abc]") = false
abc.matches("[abcd]") = false

text = "My name is Kennen";
System.out.println("text.matches(\"My [Nn]ame is Kennen\") = " + text.matches("My [Nn]ame is Kennen"));
output:
text.matches("My [Nn]ame is Kennen") = true

 

  • 표현식에 포함된 문자 1개를 의미
  • !!주의 : 1개 이상을 표현하진 않기 때문에 문자 "abc"를 "abc" 에 적용했을 경우 false이며, "abcd" 에 적용했을 경우에도 false이다.

정규표현식에 포함된 문자 1개 + 조합

Regular Expression Decription example
[expression][expression] 정규표현식에 포함된 문자 1개 조합 "af".matches("[abc][efg]")
String text = "af";
System.out.println("af.matches(\"[ab][fg]\") = " + text.matches("[ab][fg]"));
System.out.println("af.matches(\"[af]\") = " + text.matches("[af]"));
output:
af.matches("[ab][fg]") = true
af.matches("[af]") = false

text = "afyl";
System.out.println("text.matches(\"[ae][fg][yj][lm]\") = " + text.matches("[ae][fg][yj][lm]"));
output:
text.matches("[ae][fg][yj][lm]") = true
  • 반드시 포함되어야 하는 문자가 있는 경우 사용

한번에 다 써보기

String text = "My Name is Kennen";
System.out.println("text.matches(\"My [Nn]ame is .[ea][nm][nm]en\") = " + text.matches("My [Nn]ame is .[ea][nm][nm].."));
output:
text.matches("My [Nn]ame is .[ea][nm][nm]en") = true

 

+ Recent posts