Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples for Re.split function #216

Merged
merged 3 commits into from
Jul 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions lib/core.mli
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,35 @@ val matches_seq : ?pos:int -> ?len:int -> re -> string -> string Seq.t

# Re.split ~pos:3 regex "1,2,3,4. Commas go brrr.";;
- : string list = ["3"; "4. Commas go brrr."]
]}

{6 Zero-length patterns:}

Be careful when using [split] with zero-length patterns like [eol], [bow],
and [eow]. Because they don't have any width, they will still be present in
the result. (Note the position of the [\n] and space characters in the
output.)

{[
# Re.split (Re.compile Re.eol) "a\nb";;
- : string list = ["a"; "\nb"]

# Re.split (Re.compile Re.bow) "a b";;
- : string list = ["a "; "b"]

# Re.split (Re.compile Re.eow) "a b";;
- : string list = ["a"; " b"]
]}

Compare this to the behavior of splitting on the char itself. (Note that
the delimiters are not present in the output.)

{[
# Re.split (Re.compile (Re.char '\n')) "a\nb";;
- : string list = ["a"; "b"]

# Re.split (Re.compile (Re.char ' ')) "a b";;
- : string list = ["a"; "b"]
]} *)
val split : ?pos:int -> ?len:int -> re -> string -> string list

Expand All @@ -327,6 +356,35 @@ val split : ?pos:int -> ?len:int -> re -> string -> string list

# Re.split ~pos:3 regex "1,2,3,4. Commas go brrr.";;
- : string list = ["3"; "4. Commas go brrr."]
]}

{6 Zero-length patterns:}

Be careful when using [split_delim] with zero-length patterns like [eol],
[bow], and [eow]. Because they don't have any width, they will still be
present in the result. (Note the position of the [\n] and space characters
in the output.)

{[
# Re.split_delim (Re.compile Re.eol) "a\nb";;
- : string list = ["a"; "\nb"; ""]

# Re.split_delim (Re.compile Re.bow) "a b";;
- : string list = [""; "a "; "b"]

# Re.split_delim (Re.compile Re.eow) "a b";;
- : string list = ["a"; " b"; ""]
]}

Compare this to the behavior of splitting on the char itself. (Note that
the delimiters are not present in the output.)

{[
# Re.split_delim (Re.compile (Re.char '\n')) "a\nb";;
- : string list = ["a"; "b"]

# Re.split_delim (Re.compile (Re.char ' ')) "a b";;
- : string list = ["a"; "b"]
]} *)
val split_delim : ?pos:int -> ?len:int -> re -> string -> string list

Expand Down
Loading