regex: format readme examples (#7154)

pull/7157/head
Lukas Neubert 2020-12-06 02:04:07 +01:00 committed by GitHub
parent 96d28b4485
commit 79a20c338b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 36 deletions

View File

@ -182,14 +182,13 @@ for gi < re.groups.len {
**note:** *to show the `group id number` in the result of the `get_query()`*
*the flag `debug` of the RE object must be `1` or `2`*
In order to simplify the use of the captured groups it possible to use the
In order to simplify the use of the captured groups it possible to use the
utility function: `get_group_list`.
This function return a list of groups using this support struct:
```v oksyntax
pub
struct Re_group {
```v oksyntax
pub struct Re_group {
pub:
start int = -1
end int = -1
@ -198,7 +197,7 @@ pub:
Here an example of use:
```v oksyntax
```v oksyntax
/*
This simple function convert an HTML RGB value with 3 or 6 hex digits to an u32 value,
this function is not optimized and it si only for didatical purpose
@ -207,23 +206,20 @@ example: #A0B0CC #A9F
fn convert_html_rgb(in_col string) u32 {
mut n_digit := if in_col.len == 4 { 1 } else { 2 }
mut col_mul := if in_col.len == 4 { 4 } else { 0 }
// this is the regex query, it use the V string interpolation to customize the regex query
// NOTE: if you want use escaped code you must use the r"" (raw) strings,
// *** please remember that the V interpoaltion doesn't work on raw strings. ***
query:= "#([a-fA-F0-9]{$n_digit})([a-fA-F0-9]{$n_digit})([a-fA-F0-9]{$n_digit})"
// NOTE: if you want use escaped code you must use the r"" (raw) strings,
// *** please remember that the V interpoaltion doesn't work on raw strings. ***
query := '#([a-fA-F0-9]{$n_digit})([a-fA-F0-9]{$n_digit})([a-fA-F0-9]{$n_digit})'
mut re := regex.regex_opt(query) or { panic(err) }
start, end := re.match_string(in_col)
println("start: $start, end: $end")
println('start: $start, end: $end')
mut res := u32(0)
if start >= 0 {
group_list := re.get_group_list() // this is the utility function
r := ("0x" + in_col[group_list[0].start..group_list[0].end]).int() << col_mul
g := ("0x" + in_col[group_list[1].start..group_list[1].end]).int() << col_mul
b := ("0x" + in_col[group_list[2].start..group_list[2].end]).int() << col_mul
println("r: $r g: $g b: $b")
r := ('0x' + in_col[group_list[0].start..group_list[0].end]).int() << col_mul
g := ('0x' + in_col[group_list[1].start..group_list[1].end]).int() << col_mul
b := ('0x' + in_col[group_list[2].start..group_list[2].end]).int() << col_mul
println('r: $r g: $g b: $b')
res = u32(r) << 16 | u32(g) << 8 | u32(b)
}
return res
@ -413,31 +409,24 @@ struct using the function `re.get_group`.
Here a more complex example of use:
```v oksyntax
/*
This function demostrate the use of the named groups
*/
```v oksyntax
// This function demostrate the use of the named groups
fn convert_html_rgb_n(in_col string) u32 {
mut n_digit := if in_col.len == 4 { 1 } else { 2 }
mut col_mul := if in_col.len == 4 { 4 } else { 0 }
query:= "#(?P<red>[a-fA-F0-9]{$n_digit})(?P<green>[a-fA-F0-9]{$n_digit})(?P<blue>[a-fA-F0-9]{$n_digit})"
query := '#(?P<red>[a-fA-F0-9]{$n_digit})(?P<green>[a-fA-F0-9]{$n_digit})(?P<blue>[a-fA-F0-9]{$n_digit})'
mut re := regex.regex_opt(query) or { panic(err) }
start, end := re.match_string(in_col)
println("start: $start, end: $end")
println('start: $start, end: $end')
mut res := u32(0)
if start >= 0 {
red_s, red_e := re.get_group("red")
r := ("0x" + in_col[red_s..red_e]).int() << col_mul
green_s, green_e := re.get_group("green")
g := ("0x" + in_col[green_s..green_e]).int() << col_mul
blue_s, blue_e := re.get_group("blue")
b := ("0x" + in_col[blue_s..blue_e]).int() << col_mul
println("r: $r g: $g b: $b")
red_s, red_e := re.get_group('red')
r := ('0x' + in_col[red_s..red_e]).int() << col_mul
green_s, green_e := re.get_group('green')
g := ('0x' + in_col[green_s..green_e]).int() << col_mul
blue_s, blue_e := re.get_group('blue')
b := ('0x' + in_col[blue_s..blue_e]).int() << col_mul
println('r: $r g: $g b: $b')
res = u32(r) << 16 | u32(g) << 8 | u32(b)
}
return res
@ -474,14 +463,14 @@ a `RE` struct can be created manually if you needed.
#### **Simplified initializer**
```v ignore
```v ignore
// regex create a regex object from the query string and compile it
pub fn regex_opt(in_query string) ?RE
```
#### **Base initializer**
```v ignore
```v ignore
// new_regex create a REgex of small size, usually sufficient for ordinary use
pub fn new() RE