src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/mods/re/re.d.elv (about) 1 #//each:eval use re 2 3 # Quote `$string` for use in a pattern. Examples: 4 # 5 # ```elvish-transcript 6 # ~> re:quote a.txt 7 # ▶ a\.txt 8 # ~> re:quote '(*)' 9 # ▶ '\(\*\)' 10 # ``` 11 fn quote {|string| } 12 13 # Determine whether `$pattern` matches `$source`. The pattern is not anchored. 14 # Examples: 15 # 16 # ```elvish-transcript 17 # ~> re:match . xyz 18 # ▶ $true 19 # ~> re:match . '' 20 # ▶ $false 21 # ~> re:match '[a-z]' A 22 # ▶ $false 23 # ``` 24 fn match {|&posix=$false pattern source| } 25 26 # Find all matches of `$pattern` in `$source`. 27 # 28 # Each match is represented by a map-like value `$m`; `$m[text]`, `$m[start]` and 29 # `$m[end]` are the text, start and end positions (as byte indices into `$source`) 30 # of the match; `$m[groups]` is a list of submatches for capture groups in the 31 # pattern. A submatch has a similar structure to a match, except that it does not 32 # have a `group` key. The entire pattern is an implicit capture group, and it 33 # always appears first. 34 # 35 # Examples: 36 # 37 # ```elvish-transcript 38 # ~> re:find . ab 39 # ▶ [&end=(num 1) &groups=[[&end=(num 1) &start=(num 0) &text=a]] &start=(num 0) &text=a] 40 # ▶ [&end=(num 2) &groups=[[&end=(num 2) &start=(num 1) &text=b]] &start=(num 1) &text=b] 41 # ~> re:find '[A-Z]([0-9])' 'A1 B2' 42 # ▶ [&end=(num 2) &groups=[[&end=(num 2) &start=(num 0) &text=A1] [&end=(num 2) &start=(num 1) &text=1]] &start=(num 0) &text=A1] 43 # ▶ [&end=(num 5) &groups=[[&end=(num 5) &start=(num 3) &text=B2] [&end=(num 5) &start=(num 4) &text=2]] &start=(num 3) &text=B2] 44 # ``` 45 fn find {|&posix=$false &longest=$false &max=-1 pattern source| } 46 47 # Replace all occurrences of `$pattern` in `$source` with `$repl`. 48 # 49 # The replacement `$repl` can be any of the following: 50 # 51 # - A string-typed replacement template. The template can use `$name` or 52 # `${name}` patterns to refer to capture groups, where `name` consists of 53 # letters, digits and underscores. A purely numeric patterns like `$1` 54 # refers to the capture group with the corresponding index; other names 55 # refer to capture groups named with the `(?P<name>...)`) syntax. 56 # 57 # In the `$name` form, the name is taken to be as long as possible; `$1` is 58 # equivalent to `${1x}`, not `${1}x`; `$10` is equivalent to `${10}`, not `${1}0`. 59 # 60 # To insert a literal `$`, use `$$`. 61 # 62 # - A function that takes a string argument and outputs a string. For each 63 # match, the function is called with the content of the match, and its output 64 # is used as the replacement. 65 # 66 # If `$literal` is true, `$repl` must be a string and is treated literally instead 67 # of as a pattern. 68 # 69 # Example: 70 # 71 # ```elvish-transcript 72 # ~> re:replace '(ba|z)sh' '${1}SH' 'bash and zsh' 73 # ▶ 'baSH and zSH' 74 # ~> re:replace '(ba|z)sh' elvish 'bash and zsh rock' 75 # ▶ 'elvish and elvish rock' 76 # ~> re:replace '(ba|z)sh' {|x| put [&bash=BaSh &zsh=ZsH][$x] } 'bash and zsh' 77 # ▶ 'BaSh and ZsH' 78 # ``` 79 fn replace {|&posix=$false &longest=$false &literal=$false pattern repl source| } 80 81 # Split `$source`, using `$pattern` as separators. Examples: 82 # 83 # ```elvish-transcript 84 # ~> re:split : /usr/sbin:/usr/bin:/bin 85 # ▶ /usr/sbin 86 # ▶ /usr/bin 87 # ▶ /bin 88 # ~> re:split &max=2 : /usr/sbin:/usr/bin:/bin 89 # ▶ /usr/sbin 90 # ▶ /usr/bin:/bin 91 # ``` 92 fn split {|&posix=$false &longest=$false &max=-1 pattern source| } 93 94 # For each [value input](builtin.html#value-inputs), calls `$f` with the input 95 # followed by all its fields. 96 # 97 # The `&sep` option is a regular expression for the field separator. For the 98 # `&sep-posix` and `&sep-longest` options, see the 99 # [introduction](#introduction); the `sep-` prefix is added for clarity. 100 # 101 # Calling [`break`]() in `$f` exits both `$f` and `re:awk`, and can be used to 102 # stop processing inputs early. Calling [`continue`]() exits `$f` but not 103 # `re:awk`, and can be used to stop `$f` early but continue processing inputs. 104 # 105 # This command allows you to write code resembling 106 # [AWK](https://en.wikipedia.org/wiki/AWK) scripts, using an anonymous function 107 # instead of a string containing AWK code. A simple example: 108 # 109 # ```elvish-transcript 110 # ~> echo " lorem ipsum\n1 2" | awk '{ print $1 }' 111 # lorem 112 # 1 113 # ~> echo " lorem ipsum\n1 2" | re:awk {|line a b| put $a } 114 # ▶ lorem 115 # ▶ 1 116 # ``` 117 # 118 # **Note**: Since Elvish allows variable names consisting solely of digits, you 119 # can do something like this to emulate AWK even more closely: 120 # 121 # ```elvish-transcript 122 # ~> echo " lorem ipsum\n1 2" | re:awk {|0 1 2| put $1 } 123 # ▶ lorem 124 # ▶ 1 125 # ``` 126 # 127 # If the number of fields differ between lines, use a rest argument: 128 # 129 # ```elvish-transcript 130 # ~> echo "a b\nc d e" | re:awk {|@a| echo (- (count $a) 1)' fields' } 131 # 2 fields 132 # 3 fields 133 # ``` 134 # 135 # This command is roughly equivalent to the following Elvish function: 136 # 137 # ```elvish 138 # fn my-awk {|&sep='[ \t]+' &sep-posix=$false &sep-longest=$false f @rest| 139 # each {|line| 140 # var @fields = (re:split $sep &posix=$sep-posix &longest=$sep-longest (str:trim $line " \t")) 141 # $f $line $@fields 142 # } $@rest 143 # } 144 # ``` 145 fn awk {|&sep='[ \t]+' &sep-posix=$false &sep-longest=$false f inputs?| }