본문으로 건너뛰기
토파즈6.0 · 출시 전
다운로드 →

문서 검색

명세 6.0

방향키로 결과를 고르고 Enter를 눌러 여세요.

API 이름, 진단 번호, 규칙 또는 프로젝트 주제를 입력하세요.

    라이브러리 · std.regex

    Regex.replaceAll

    Replaces the selected matches and expands braced capture references.

    replaceAll(text: string, replacement: string) -> string
    수신자
    Regex
    최소 인자 수
    2
    최대 인자 수
    2

    Declaration and parameters

    링크

    사용 조건

    기본 포함 · 미정

    Import std.regex in language mode 5.21.

    The current compiler selects std/regex.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.

    수신자

    Regex

    매개변수 제약

    Regex.split returns the input portions between the matches selected by findAll. Delimiters disappear and capture groups are not inserted. Empty portions at either end and between adjacent delimiters are retained. No match produces an array containing the original input.

    Regex.replaceAll replaces the same nonoverlapping matches while copying all text between them unchanged. No match returns the original text. The operation does not search inserted replacement text.

    Replacement references use a dollar sign followed by a braced capture number or name. Capture zero denotes the complete match. An unknown or nonparticipating capture expands to an empty string. Two dollar signs produce one literal dollar.

    A replacement selector is a nonnegative decimal capture number or an ASCII capture name. Dollar text that is neither a double dollar nor a well-formed braced selector is literal. Unbraced dollar names and numbers do not form references. Expansion is performed once and captured dollar text is not expanded again.

    An empty pattern splits at every scalar boundary and inserts replacement text at those same boundaries. The adjacent-empty-match rule from findAll also applies when a nonempty match is followed by an empty candidate.

    The receiver is evaluated once before text and replacement arguments, which are evaluated once from left to right. Splitting and replacement leave the original string and Regex unchanged. They have no external effects and do not call a user callback.

    결과

    The declared result type is string.

    효과

    실패

    Result and effects

    링크

    Replaces the selected matches and expands braced capture references.

    Resolved contracts

    링크

    Related entries

    링크

    std.regex →

    Split and replace using the shared match sequence

    링크
    규칙topaz.stdlib.contract.regex-transform.rule

    Split and replace using the shared match sequence

    Regex.split returns the input portions between the matches selected by findAll. Delimiters disappear and capture groups are not inserted. Empty portions at either end and between adjacent delimiters are retained. No match produces an array containing the original input.

    Regex.replaceAll replaces the same nonoverlapping matches while copying all text between them unchanged. No match returns the original text. The operation does not search inserted replacement text.

    Replacement references use a dollar sign followed by a braced capture number or name. Capture zero denotes the complete match. An unknown or nonparticipating capture expands to an empty string. Two dollar signs produce one literal dollar.

    A replacement selector is a nonnegative decimal capture number or an ASCII capture name. Dollar text that is neither a double dollar nor a well-formed braced selector is literal. Unbraced dollar names and numbers do not form references. Expansion is performed once and captured dollar text is not expanded again.

    An empty pattern splits at every scalar boundary and inserts replacement text at those same boundaries. The adjacent-empty-match rule from findAll also applies when a nonempty match is followed by an empty candidate.

    The receiver is evaluated once before text and replacement arguments, which are evaluated once from left to right. Splitting and replacement leave the original string and Regex unchanged. They have no external effects and do not call a user callback.

    이유

    Using one match sequence prevents a search from disagreeing with a later replacement. Braced capture references have one spelling and an explicit end, so a capture name cannot absorb following replacement text.

    Retain empty split fields without inserting captures

    링크
    main.tpzRetain empty split fields without inserting captures플레이그라운드 열기 →
    1. import std.regex { Regex }
    2. import std.io
    3. match (Regex.compile("(,)")) {
    4. case Ok(pattern) => {
    5. for field in (pattern.split(",a,,b,")) {
    6. io.writeLine("[{field}]")
    7. }
    8. for field in (pattern.split("plain")) {
    9. io.writeLine("[{field}]")
    10. }
    11. }
    12. case Err(_) => io.writeLine("compile failed")
    13. }
    기대 출력종료 코드 0
    []
    [a]
    []
    [b]
    []
    [plain]
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. []
    2. [a]
    3. []
    4. [b]
    5. []
    6. [plain]

    Expand braced capture references and a literal dollar

    링크
    main.tpzExpand braced capture references and a literal dollar플레이그라운드 열기 →
    1. import std.regex { Regex }
    2. import std.io
    3. match (Regex.compile("(?<word>[a-z]+)([0-9]+)?")) {
    4. case Ok(pattern) => {
    5. let replacement = "$\{word\}/$\{0\}/$\{2\}/$\{missing\}/$$"
    6. io.writeLine(pattern.replaceAll("a1 b", replacement))
    7. io.writeLine(pattern.replaceAll("123", replacement))
    8. }
    9. case Err(_) => io.writeLine("compile failed")
    10. }
    기대 출력종료 코드 0
    a/a1/1//$ b/b///$
    123
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. a/a1/1//$ b/b///$
    2. 123

    Preserve text that is not a braced reference

    링크
    main.tpzPreserve text that is not a braced reference플레이그라운드 열기 →
    1. import std.regex { Regex }
    2. import std.io
    3. match (Regex.compile("a")) {
    4. case Ok(pattern) => io.writeLine(pattern.replaceAll("a", "$word $1 $ $\{\} $\{bad-name\}"))
    5. case Err(_) => io.writeLine("compile failed")
    6. }
    기대 출력종료 코드 0
    $word $1 $ ${} ${bad-name}
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. $word $1 $ ${} ${bad-name}

    Split and replace at each empty-pattern boundary

    링크
    main.tpzSplit and replace at each empty-pattern boundary플레이그라운드 열기 →
    1. import std.regex { Regex }
    2. import std.io
    3. match (Regex.compile("")) {
    4. case Ok(pattern) => {
    5. for field in (pattern.split("ab")) {
    6. io.writeLine("[{field}]")
    7. }
    8. io.writeLine(pattern.replaceAll("ab", "-"))
    9. }
    10. case Err(_) => io.writeLine("compile failed")
    11. }
    기대 출력종료 코드 0
    []
    [a]
    [b]
    []
    -a-b-
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. []
    2. [a]
    3. [b]
    4. []
    5. -a-b-

    Share the adjacent-empty-match rule across transformations

    링크
    main.tpzShare the adjacent-empty-match rule across transformations플레이그라운드 열기 →
    1. import std.regex { Regex }
    2. import std.io
    3. match (Regex.compile("a*")) {
    4. case Ok(pattern) => {
    5. for field in (pattern.split("ab")) {
    6. io.writeLine("[{field}]")
    7. }
    8. io.writeLine(pattern.replaceAll("ab", "-"))
    9. }
    10. case Err(_) => io.writeLine("compile failed")
    11. }
    기대 출력종료 코드 0
    []
    [b]
    []
    -b-
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. []
    2. [b]
    3. []
    4. -b-

    Evaluate receiver and replacement inputs once in source order

    링크
    main.tpzEvaluate receiver and replacement inputs once in source order플레이그라운드 열기 →
    1. import std.regex { Regex }
    2. import std.test
    3. import std.io
    4. function makePattern() -> Regex {
    5. io.writeLine("receiver")
    6. return test.assertOk(Regex.compile("a"))
    7. }
    8. function inputText() -> string {
    9. io.writeLine("text")
    10. return "a"
    11. }
    12. function replacementText() -> string {
    13. io.writeLine("replacement")
    14. return "b"
    15. }
    16. io.writeLine(makePattern().replaceAll(inputText(), replacementText()))
    기대 출력종료 코드 0
    receiver
    text
    replacement
    b
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. receiver
    2. text
    3. replacement
    4. b

    메뉴

    다운로드 →