src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/mods/re/re_test.elvts (about)

     1  //each:eval use re
     2  
     3  ////////////
     4  # re:match #
     5  ////////////
     6  
     7  ~> re:match . xyz
     8  ▶ $true
     9  ~> re:match . ''
    10  ▶ $false
    11  ~> re:match '[a-z]' A
    12  ▶ $false
    13  
    14  ## invalid pattern ##
    15  ~> re:match '(' x
    16  Exception: error parsing regexp: missing closing ): `(`
    17    [tty]:1:1-14: re:match '(' x
    18  
    19  ///////////
    20  # re:find #
    21  ///////////
    22  
    23  ~> re:find . ab
    24  ▶ [&end=(num 1) &groups=[[&end=(num 1) &start=(num 0) &text=a]] &start=(num 0) &text=a]
    25  ▶ [&end=(num 2) &groups=[[&end=(num 2) &start=(num 1) &text=b]] &start=(num 1) &text=b]
    26  ~> re:find '[A-Z]([0-9])' 'A1 B2'
    27  ▶ [&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]
    28  ▶ [&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]
    29  
    30  ## access to fields in the match StructMap ##
    31  ~> put (re:find . a)[text start end groups]
    32  ▶ a
    33  ▶ (num 0)
    34  ▶ (num 1)
    35  ▶ [[&end=(num 1) &start=(num 0) &text=a]]
    36  
    37  ## invalid pattern ##
    38  ~> re:find '(' x
    39  Exception: error parsing regexp: missing closing ): `(`
    40    [tty]:1:1-13: re:find '(' x
    41  
    42  ## without any flag, finds ax ##
    43  ~> put (re:find 'a(x|xy)' AaxyZ)[text]
    44  ▶ ax
    45  
    46  ## with &longest, finds axy ##
    47  ~> put (re:find &longest 'a(x|xy)' AaxyZ)[text]
    48  ▶ axy
    49  
    50  ## basic verification of &posix behavior. ##
    51  ~> put (re:find &posix 'a(x|xy)+' AaxyxxxyZ)[text]
    52  ▶ axyxxxy
    53  
    54  ## bubbles output error ##
    55  ~> re:find . ab >&-
    56  Exception: port does not support value output
    57    [tty]:1:1-16: re:find . ab >&-
    58  
    59  //////////////
    60  # re:replace #
    61  //////////////
    62  
    63  ~> re:replace '(ba|z)sh' '${1}SH' 'bash and zsh'
    64  ▶ 'baSH and zSH'
    65  ~> re:replace &literal '(ba|z)sh' '$sh' 'bash and zsh'
    66  ▶ '$sh and $sh'
    67  ~> re:replace '(ba|z)sh' {|x| put [&bash=BaSh &zsh=ZsH][$x] } 'bash and zsh'
    68  ▶ 'BaSh and ZsH'
    69  
    70  ## invalid pattern ##
    71  ~> re:replace '(' x bash
    72  Exception: error parsing regexp: missing closing ): `(`
    73    [tty]:1:1-21: re:replace '(' x bash
    74  ~> re:replace &posix '[[:argle:]]' x bash
    75  Exception: error parsing regexp: invalid character class range: `[:argle:]`
    76    [tty]:1:1-38: re:replace &posix '[[:argle:]]' x bash
    77  
    78  ## replacement function outputs more than one value ##
    79  ~> re:replace x {|x| put a b } xx
    80  Exception: arity mismatch: replacement function output must be 1 value, but is 2 values
    81    [tty]:1:1-30: re:replace x {|x| put a b } xx
    82  
    83  ## replacement function outputs non-string value ##
    84  ~> re:replace x {|x| put [] } xx
    85  Exception: bad value: replacement function output must be string, but is list
    86    [tty]:1:1-29: re:replace x {|x| put [] } xx
    87  
    88  ## replacement is not string or function ##
    89  ~> re:replace x [] xx
    90  Exception: bad value: replacement must be string or function, but is list
    91    [tty]:1:1-18: re:replace x [] xx
    92  
    93  ## replacement is function when &literal is set ##
    94  ~> re:replace &literal x {|_| put y } xx
    95  Exception: bad value: literal replacement must be string, but is fn
    96    [tty]:1:1-37: re:replace &literal x {|_| put y } xx
    97  
    98  ////////////
    99  # re:split #
   100  ////////////
   101  
   102  ~> re:split : /usr/sbin:/usr/bin:/bin
   103  ▶ /usr/sbin
   104  ▶ /usr/bin
   105  ▶ /bin
   106  ~> re:split &max=2 : /usr/sbin:/usr/bin:/bin
   107  ▶ /usr/sbin
   108  ▶ /usr/bin:/bin
   109  
   110  ## invalid pattern ##
   111  ~> re:split '(' x
   112  Exception: error parsing regexp: missing closing ): `(`
   113    [tty]:1:1-14: re:split '(' x
   114  
   115  ## bubbles output error ##
   116  ~> re:split . ab >&-
   117  Exception: port does not support value output
   118    [tty]:1:1-17: re:split . ab >&-
   119  
   120  ////////////
   121  # re:quote #
   122  ////////////
   123  
   124  ~> re:quote a.txt
   125  ▶ a\.txt
   126  ~> re:quote '(*)'
   127  ▶ '\(\*\)'