Regexp

Go like Regular expressions

regexp

Creates a compiled regular expression object from a pattern string.

pattern String containing a regular expression pattern

returns native regexp object or error if pattern is invalid

regexp "[0-9]" |type?
; returns native

regexp//Is-match

Tests if a string matches the regular expression pattern.

regexp Native regexp object

input String to test against the pattern

returns integer 1 if the string matches the pattern, 0 otherwise

regexp "[0-9]" |Is-match "5"
; returns 1
regexp "[0-9]" |Is-match "a"
; returns 0

regexp//Submatch?

Extracts the first captured group from a string using the regular expression.

regexp Regular expression with capturing groups

input String to search in

returns string containing the first captured group or error if no submatch found

regexp "x([0-9]+)y" |Submatch? "x123y"
; returns "123"

regexp//Submatches?

Extracts all captured groups from the first match as a block of strings.

regexp Regular expression with capturing groups

input String to search in

returns block containing all captured groups from the first match or error if no match found

regexp "x([0-9]+)y" |Submatches? "x123y x234y"
; returns { "123" }

regexp//Submatches\all?

Extracts all captured groups from all matches as a nested block structure.

regexp Regular expression with capturing groups

input String to search in

returns block of blocks, each inner block containing the captured groups from one match

regexp "x([0-9]+)(y+)?" |Submatches\all? "x11yy x22"
; returns { { "11" "yy" } { "22" "" } }

regexp//Find-all

Finds all substrings matching the regular expression and returns them as a block.

regexp Regular expression pattern

input String to search in

returns block containing all matching substrings or error if no matches found

regexp "[0-9]+" |Find-all "x123y x234y"
; returns { "123" "234" }

regexp//Match?

Finds the first substring matching the regular expression.

regexp Regular expression pattern

input String to search in

returns string containing the first match or empty string if no match found

regexp "[0-9]+c+" |Match? "aa33bb55cc"
; returns "55cc"

regexp//Replace-all

Replaces all occurrences of the regular expression pattern with the specified replacement string.

regexp Regular expression pattern

input String to modify

replacement String to replace matches with

returns string with all matches replaced by the replacement string

regexp "[0-9]+" |Replace-all "x123y x234y" "XXX"
; returns "xXXXy xXXXy"

Validation

validation dialect for Rye values

validate

Validates and transforms data according to specified rules, returning a dictionary with converted values or an error.

data Dictionary, Context, or List to validate

rules Block containing validation rules

returns validated Dictionary/Context/List with converted values or error if validation fails

validate>ctx

Validates and transforms data according to specified rules, returning a context object for easy field access.

data Dictionary to validate

rules Block containing validation rules

returns validated Context with converted values or error if validation fails

{map[a:{1}] {0}}
validate>ctx dict { a: 1 } { a: required } |type?
; returns ctx
{map[a:{1}] {0}}
validate>ctx dict { a: 1 } { a: optional 0 } |-> 'a
; returns 1

JSON

Parsing and generating JSON

parse-json

Parses JSON string into Rye values.

json string containing JSON data

returns parsed Rye value (list, dict, string, integer, etc.)

"[ 1, 2, 3 ]" |parse-json |length?
; returns 3
"[ 1, 2, 3 ]" |parse-json |type?
; returns list

to-json

Converts a Rye value to a JSON string.

value any Rye value to encode (list, dict, string, integer, etc.)

returns string containing the JSON representation

list { 1 2 3 } |to-json
; returns "[1, 2, 3] "
dict { a: 1 b: 2 c: 3 } |to-json
; returns "{"a": 1, "b": 2, "c": 3} "

to-json\lines

Converts a table to JSON with each row on a separate line.

table table value to encode

returns string containing the JSON representation with each row on a new line

table { "a" "b" } { 2 "x" 3 "y" } |to-json\lines
; returns "{"a": 2, "b": "x"} 
; {"a": 3, "b": "y"} 
; "

BSON

BSON encoding and decoding

from-bson

Decodes BSON data into Rye values.

bytes native bytes object containing BSON data

returns decoded Rye value (string, integer, decimal, block, etc.)

"abc" |to-bson |from-bson
; returns "abc"
123 |to-bson |from-bson
; returns 123
{ 123 "asd" } |to-bson |from-bson
; returns { 123 "asd" }

to-bson

Encodes a Rye value into BSON format.

value any Rye value to encode (string, integer, decimal, block, etc.)

returns native bytes object containing the BSON-encoded data

"abc" |to-bson |type?
; returns native
"abc" |to-bson |kind?
; returns bytes

SXML

Streaming, SAX-like XML processing

reader//do-sxml

Processes XML using a streaming SAX-like approach with tag handlers.

reader XML reader object

block SXML processing block with tag handlers

returns result of processing the XML

"<scene><bot>C3PO</bot><bot>R2D2</bot><jedi>Luke</jedi></scene>" |reader .do-sxml { _ [ .prns ] }
; prints "C3PO R2D2 Luke "
"<scene><bot>C3PO</bot><bot>R2D2</bot><jedi>Luke</jedi></scene>" |reader .do-sxml { <bot> { _ [ .prns ] } }
; prints "C3PO R2D2 "
"<scene><ship>XWing</ship><bot>R2D2</bot><jedi>Luke</jedi></scene>" |reader .do-sxml { <bot> <jedi> { _ [ .prns ] } }
; prints "R2D2 Luke "
"<scene><xwing><bot>R2D2</bot><person>Luke</person></xwing><destroyer><person>Vader</person></destroyer></scene>" |reader .do-sxml { <xwing> { <person> { _ [ .prns ] } } }
; prints "Luke "

rye-sxml-start//attr?

Retrieves an attribute value by index from an XML start element.

element XML start element

index Integer index of the attribute to retrieve

returns string value of the attribute or void if not found

"<scene><ship type="xwing"><person age="25">Luke</person></ship><ship type="destroyer"><person age="55">Vader</person></ship></scene>" |reader .do-sxml { <ship> [ .attr? 0 |prns ] }
; prints "xwing destroyer "
"<scene><ship type="xwing"><person age="25">Luke</person></ship><ship type="destroyer"><person age="55">Vader</person></ship></scene>" |reader .do-sxml { <person> [ .attr? 0 |prns ] }
; prints "25 55 "

rye-sxml-start//name?

Returns the name of an XML start element.

element XML start element

returns string name of the XML element

HTML

HTML processing functions

unescape\html

Converts HTML entities to their character equivalents.

text HTML-escaped string

returns string with HTML entities converted to their character equivalents

unescape\html "&gt;hello&lt;"
; returns ">hello<"

escape\html

Converts special characters to HTML entities.

text String containing HTML special characters

returns string with special characters converted to HTML entities

escape\html "<hello>"
; returns "&lt;hello&gt;"

html->markdown

Converts HTML text to markdown format.

html HTML string to convert

returns string containing markdown equivalent of the HTML

html->markdown "<h1>title</h1><p>para</p>"
; returns "# title
; 
; para"

reader//parse-html

Parses HTML using a streaming approach with tag handlers.

reader HTML reader object

block HTML processing block with tag handlers

returns result of processing the HTML

"<html><body><div class='menu'><a href='/'>home</a><a href='/about/'>about</a>" |reader .parse-html { <a> [ .attr? 'href |prns ] }
; prints "/ /about/ "

rye-html-start//attr?

Retrieves an attribute value by name or index from an HTML element.

element HTML token element

name-or-index Attribute name (as word) or index (as integer)

returns string value of the attribute or void if not found

"<div class='menu' id='nav'></div>" |reader .parse-html { <div> [ .attr? 'class |prn ] }
; prints "menu"
"<div class='menu' id='nav'></div>" |reader .parse-html { <div> [ .attr? 'id |prn ] }
; prints "nav"

rye-html-start//name?

Returns the name of an HTML element.

element HTML token element

returns string name of the HTML element