IT/Regular Expressions
[정규식] 그룹(group) 재참조
체계성
2021. 12. 5. 19:33
- (?<name>capturing text) to define a named group "name"
- \k<name> to backreference a named group "name"
- ${name} to reference to captured group in Matcher's replacement string
- Matcher.group(String name) to return the captured input subsequence by the given "named group".
출처 : https://stackoverflow.com/questions/415580/regex-named-groups-in-java
Regex Named Groups in Java
It is my understanding that the java.regex package does not have support for named groups (http://www.regular-expressions.info/named.html) so can anyone point me towards a third-party library that ...
stackoverflow.com
명명된 캡처링 그룹화(?<name>x), \k<name>)
ES2018에서 캡처링 그룹에 명시적으로 이름을 명명할 수 있는 기능이 추가되었습니다. 아래와 같은 문법으로 사용합니다.
const str = "aaabbbabab";
// 아래 문법은 `ab`를 `foo`라는 이름으로 캡처하고,
// `\k<foo>`라는 곳에서 해당 이름을 호출하여 전체 정규식이 `abab`가 된 모습입니다
str.match(/(?<foo>ab)\k<foo>/);
// (2) ["abab", "ab", index: 6, input: "aaabbbabab", groups: {…}]