1
{/} {/} {{}}이 정규 표현식은 나중에 일치하는 표현식 (예 :/{name}/{age})에 정규 표현식 그룹 (? < 단어> \ w ++)으로 바꿔야합니다. 내가 교체의 결과를 얻을 때 아무것도이 대체 코드의 문제점은 무엇입니까?
는 그러나 :(:
String p = "/{name}/{id}";
p = p.replaceAll("\\{(\\w+)\\}", "(?<$1>\\\\\\\\w+)");
Pattern URL_PATTERN = Pattern.compile(p);
CharSequence cs = "/lucas/3";
Matcher m = URL_PATTERN.matcher(cs);
if(m.matches()){
for(int i=1;i<m.groupCount();++i){
System.out.println(m.group("name"));
}
}
는 결과/(? \ + w)/(? \ + w)와()는 Pattern.compile에 넣어이 작동하지 않습니다 :
String p = "/{name}/{id}";
p = p.replaceAll("\\{(\\w+)\\}", "(?<$1>\\\\\\\\w+)");
Pattern URL_PATTERN = Pattern.compile("/(?<name>\\w+)/(?<id>\\w+)");
System.out.println(p);
CharSequence cs = "/lucas/3";
Matcher m = URL_PATTERN.matcher(cs);
if(m.matches()){
for(int i=1;i<m.groupCount();++i){
System.out.println(m.group("name"));
}
}
결과 :? "루카스"
잘못 무엇
그것은 작동합니다! :) 감사! –
다행히 도울 수 있어요 :) – Pshemo