re.replace
re.replace(stringText, replaceRegex, replacementText)
설명
정규 표현식 교체를 수행합니다.
이 함수에는 3개의 인수가 있습니다.
stringText: 원래 문자열입니다.replaceRegex: 검색할 패턴을 나타내는 정규 표현식입니다.replacementText: 각 일치 항목에 삽입할 텍스트입니다.
원래 stringText에서 파생된 새 문자열을 반환합니다. 여기서 replaceRegex의 패턴과 일치하는 모든 하위 문자열은 replacementText의 값으로 바뀝니다. replacementText 내에 백슬래시로 이스케이프 처리된 숫자(\1~\9)를 사용하여 replaceRegex 패턴에서 괄호로 묶인 해당 그룹과 일치하는 텍스트를 삽입할 수 있습니다. 전체 일치 텍스트를 나타내려면 \0을 사용합니다.
이 함수는 겹치지 않는 일치 항목을 교체하며 발견된 첫 번째 일치 항목을 교체하는 데 우선순위를 둡니다. 예를 들어 re.replace("banana", "ana", "111")는 'b111na' 문자열을 반환합니다.
매개변수 데이터 유형
STRING, STRING, STRING
반환 유형
STRING
코드 샘플
예 1
이 예시에서는 이메일에서 @ 기호 다음에 있는 모든 것을 캡처하고 com을 org로 바꾼 후 해당 결과를 반환합니다. 중첩 함수 사용을 확인하세요.
"email@google.org" = re.replace($e.network.email.from, "com", "org")
예 2
이 예시에서는 replacementText 인수에 백슬래시로 이스케이프 처리된 숫자를 사용하여 replaceRegex 패턴과의 일치 항목을 참조합니다.
"test1.com.google" = re.replace(
$e.principal.hostname, // holds "test1.test2.google.com"
"test2\.([a-z]*)\.([a-z]*)",
"\\2.\\1" // \\1 holds "google", \\2 holds "com"
)
예 3
빈 문자열과 re.replace()를 처리할 때는 다음 사례에 유의하세요.
빈 문자열을 replaceRegex로 사용:
// In the function call below, if $e.principal.hostname contains "name",
// the result is: 1n1a1m1e1, because an empty string is found next to
// every character in `stringText`.
re.replace($e.principal.hostname, "", "1")
빈 문자열을 바꾸려면 "^$"를 replaceRegex로 사용:
// In the function call below, if $e.principal.hostname contains the empty
// string, "", the result is: "none".
re.replace($e.principal.hostname, "^$", "none")