| Line 127: | Line 127: | ||
REGEX_REPLACE("ab12cd34","[0-9]+","($0)") | REGEX_REPLACE("ab12cd34","[0-9]+","($0)") | ||
ab(12)cd(34) | ab(12)cd(34) | ||
* <tt>REGEX_SEARCH_STR<tt> searches for the **first** occurence of a pattern in a string. | * <tt>REGEX_SEARCH_STR</tt> searches for the **first** occurence of a pattern in a string. | ||
REGEX_SEARCH_STR("abcdef000234daf","[1-9][0-9]*") | REGEX_SEARCH_STR("abcdef000234daf","[1-9][0-9]*") | ||
"234" | "234" | ||
ProB supports the STRING data type also provided by Atelier-B. However, ProB provides considerable additional features described below.
"astring" a specific (single-line) string value
'''astring''' an alternate way of writing (multi-line) strings, no need to escape "
```tstring``` template strings, where ${Expr} or $«Expr» parts are evaluated and converted to string,
you can provide options separated by commas in square brackets like $[2f]{Expr}.
Valid options are: Nf (for floats/reals), Nd (for integer), Np (padding),
ascii (can be abbreviated to a), unicode (can be abbreviated to u).
ProB supports the following escape sequences within strings:
\n newline (ASCII character 13) \r carriage return (ASCII 10) \t tab (ASCII 9) \" the double quote symbol " \' the single quote symbol ' \\ the backslash symbol
Within single-line string literals, you do not need to escape '. Within multi-line string literals, you do not need to escape " and you can use tabs and newlines.
ProB assumes that all B machines and strings use the UTF-8 encoding.
Atelier-B does not support any operations on strings, apart from equality and disequality. In ProB, however, some of the sequence operators work also on strings:
size(s) the length of a string s rev(s) the reverse of a string s s ^ t the concatenation of two strings conc(ss) the concatenation of a sequence of strings
You can turn this support off using the STRING_AS_SEQUENCE preference.
ProB provides various external functions to manipulate strings.
You can obtain the definitions of this library by putting the following into your DEFINITIONS clause:
DEFINITIONS "LibraryStrings.def"
The file LibraryStrings.def is bundled with ProB and can be found in the stdlib folder. You can also include the machine LibraryStrings.mch instead of the definition file; the machine defines some of the functions below as proper B functions (i.e., functions for which you can compute the domain and use constructs such as relational image).
Below are a few of the provided external functions along with some example uses:
STRING_APPEND("abc","abc")
"abcabc"
STRING_LENGTH("abc")
3
STRING_SPLIT("usr/local/lib","/")
{(1↦"usr"),(2↦"local"),(3↦"lib")}
It is the inverse of the STRING_SPLIT function.
STRING_JOIN(["usr","local","lib"],"/") "usr/local/lib"
CODES_TO_STRING([65,66,67]) "ABC"
STRING_EQUAL_CASE_INSENSITIVE("aOuB","AoUB")
TRUE
SUB_STRING("abcdefg",1,3)
"abc"
DEC_STRING_TO_INT("1024",2)
102400
STRING_PADLEFT("10",5,"0")
"00010"
STRING_REPLACE("a.bc.d",".","->")
"a->bc->d"
LibraryRegex.def: providing access to regular expression operators on strings (REGEX_MATCH, REGEX_REPLACE, REGEX_SEARCH,...)
This library provides various facilities for pattern matching with regular expressions. You can obtain the definitions below by putting the following into your DEFINITIONS clause:
DEFINITIONS "LibraryRegex.def"
The file LibraryRegex.def is also bundled with ProB and can be found in the stdlib folder (as of version 1.8.3-beta4).
The library works on B strings and regular expression patterns are also written as B strings. The syntax used is the ECMAScript syntax: [1]. The library is currently implemented using the C++ standard library. Below we repeat some information from [2] for convenience.
The library now does support UTF-8 encoded strings and patterns. Note that ProB only supports UTF-8 for B machines and for any strings and Unicode files it processes.
REGEX_MATCH("abc","[a-z]+")
TRUE
REGEX_REPLACE("a01b23c4d56","[0-9]+","NUM")
"aNUMbNUMcNUMdNUM"
REGEX_REPLACE("1abd00abc2","([a-z]+).*?([a-z]+)","<<$2$1>>")
1<<abcabd>>2
REGEX_REPLACE("ab12cd34","[0-9]+","($0)")
ab(12)cd(34)
REGEX_SEARCH_STR("abcdef000234daf","[1-9][0-9]*")
"234"
REGEX_SEARCH("abcdef000234daf",1,"alpha:+")
rec(length:6,position:1,string:"abcdef",submatches:∅)
REGEX_SEARCH_ALL("abcdef000234daf567","([1-9])([0-9]*)")
{(1↦"234"),(2↦"567")}
As of ProB 1.12.0 the above functions also have counterparts which ignore the case. The names of the functions have and additional I (for Ignore case). Here are a few examples to illustrate their behaviour.
REGEX_IMATCH("abC","(a|b|c)+")
TRUE
REGEX_ISEARCH("abCabCdABC",1,"(a|b|c)+")'string
"abCabC"
REGEX_ISEARCH_STR("abCabCdABC","(a|b|c)+")
"abCabC"
REGEX_ISEARCH_ALL("abCabCdABC","(a|b|c)+")
{(1↦"abCabC"),(2↦"ABC")}
REGEX_IREPLACE("abCabCdABC","(a|b|c)+","*")
"*d*"