user
Task[edit]

Write a function or program that can split a string at each non-escaped occurrence of a separator character.

It should accept three input parameters:

:   :   :   * The **string**
            * The **separator character**
            * The **escape character**

It should output a list of strings.

Details

Rules for splitting:

* The fields that were separated by the separators, become the elements of the output list.
* Empty fields should be preserved, even at the start and end.

Rules for escaping:

* "Escaped" means preceded by an occurrence of the escape character that is not already escaped itself.
* When the escape character precedes a character that has no special meaning, it still counts as an escape (but does not do anything special).
* Each occurrence of the escape character that was used to escape something, should **not** become part of the output.

Test case

Demonstrate that your function satisfies the following test-case:

| Input | Output |
| --- | --- |
| |  |  | | --- | --- | | string: | ``` one^|uno||three^^^^|four^^^|^cuatro| ``` | | separator character: | ``` | ``` | | escape character: | ``` ^ ``` | | |  | | --- | | ``` one|uno ``` | |  | | ``` three^^ ``` | | ``` four^|cuatro ``` | |  | |

(Print the output list in any format you like, as long as it is it easy to see what the fields are.)

Other tasks related to string operations:

Metrics

* Array length
* String length
* Copy a string
* Empty string  (assignment)

Counting

* Word frequency
* Letter frequency
* Jewels and stones
* I before E except after C
* Bioinformatics/base count
* Count occurrences of a substring
* Count how many vowels and consonants occur in a string

Remove/replace

* XXXX redacted
* Conjugate a Latin verb
* Remove vowels from a string
* String interpolation (included)
* Strip block comments
* Strip comments from a string
* Strip a set of characters from a string
* Strip whitespace from a string -- top and tail
* Strip control codes and extended characters from a string

Anagrams/Derangements/shuffling

* Word wheel
* ABC problem
* Sattolo cycle
* Knuth shuffle
* Ordered words
* Superpermutation minimisation
* Textonyms (using a phone text pad)
* Anagrams
* Anagrams/Deranged anagrams
* Permutations/Derangements

Find/Search/Determine

* ABC words
* Odd words
* Word ladder
* Semordnilap
* Word search
* Wordiff  (game)
* String matching
* Tea cup rim text
* Alternade words
* Changeable words
* State name puzzle
* String comparison
* Unique characters
* Unique characters in each string
* Extract file extension
* Levenshtein distance
* Palindrome detection
* Common list elements
* Longest common suffix
* Longest common prefix
* Compare a list of strings
* Longest common substring
* Find common directory path
* Words from neighbour ones
* Change e letters to i in words
* Non-continuous subsequences
* Longest common subsequence
* Longest palindromic substrings
* Longest increasing subsequence
* Words containing "the" substring
* Sum of the digits of n is substring of n
* Determine if a string is numeric
* Determine if a string is collapsible
* Determine if a string is squeezable
* Determine if a string has all unique characters
* Determine if a string has all the same characters
* Longest substrings without repeating characters
* Find words which contains all the vowels
* Find words which contain the most consonants
* Find words which contains more than 3 vowels
* Find words whose first and last three letters are equal
* Find words with alternating vowels and consonants

Formatting

* Substring
* Rep-string
* Word wrap
* String case
* Align columns
* Literals/String
* Repeat a string
* Brace expansion
* Brace expansion using ranges
* Reverse a string
* Phrase reversals
* Comma quibbling
* Special characters
* String concatenation
* Substring/Top and tail
* Commatizing numbers
* Reverse words in a string
* Suffixation of decimal numbers
* Long literals, with continuations
* Numerical and alphabetical suffixes
* Abbreviations, easy
* Abbreviations, simple
* Abbreviations, automatic

Song lyrics/poems/Mad Libs/phrases

* Mad Libs
* Magic 8-ball
* 99 bottles of beer
* The Name Game (a song)
* The Old lady swallowed a fly
* The Twelve Days of Christmas

Tokenize

* Text between
* Tokenize a string
* Word break problem
* Tokenize a string with escaping
* Split a character string based on change of character

Sequences

* Show ASCII table
* De Bruijn sequences
* Summarize and say sequence
* Generate lower case ASCII alphabet
assistant
(formerly Perl 6)

```
sub tokenize ($string, :$sep!, :$esc!) {
    return $string.match(/([ <!before $sep | $esc> . | $esc . ]*)+ % $sep/)\
                  .[0].map(*.subst: /$esc )> ./, '', :g);
}

say "'$_'" for tokenize 'one^|uno||three^^^^|four^^^|^cuatro|', sep => '|', esc => '^';

```

Output:

```
'one|uno'
''
'three^^'
'four^|cuatro'
''

```

Notable Raku innovations that make this different from the equivalent #Perl solution:

* string variables can be safely interpolated into regexes without having to 'quotemeta' them
* regexes matches return a nested `Match` object which allows retrieving *all* results for a given capture group *(rather than just the last thing that it matched)*, thus getting rid of the need for repeated global matching
* the `<field>+ % <delimiter>` regex construct allows handling the delimiters in a more idiomatic way
* the `)>` regex construct can be used to exclude anything that follows it from the returned match result