[Java] 정규 표현식 - matches
string내에서 원하는 부분을 찾기 위해 정규표현식과 string함수 중 string.matches(정규표현식)을 사용하였다.
하지만 string에서 해당 정규표현식을 찾지 못하였다.
그 이유는 matches는 전체 스트링이 해당 정규표현식을 만족했어야하기 때문에 찾지 못하는것이었다.
내가 찾으려는거 같은 경우는 \n이 포함되어 있는 여러 줄로 되어있는 string이었는데
이건 .*에 포함되지 않아서 안됬었다.
자세한 내용은 아래링크 참조
https://stackoverflow.com/questions/8923398/regex-doesnt-work-in-string-matches
Regex doesn't work in String.matches()
I have this small piece of code String[] words = {"{apf","hum_","dkoe","12f"}; for(String s:words) { if(s.matches("[a-z]")) { System.out.println(s); } } Supposed to print dk...
stackoverflow.com
Pattern p = Pattern.compile("[a-z]");
Matcher m = p.matcher(inputstring);
if (m.find())
// match
matches가 아닌 위의 코드의 방법을 사용하면 예상한대로 결과를 얻을 수 있었다.
RegExr: Learn, Build, & Test RegEx
RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).
regexr.com
위 사이트에서 정규표현식을 입력하면 바로 확인이 가능해서 편리하였다.