Skip to content

core.texttools.regext #

regex

basic regex utilities

  • .

regex replacer

Tool to flexibly replace elements in file(s) or text.

next example does it for

import freeflowuniverse.crystallib.core.texttools.regext
text := '

this is test_1 SomeTest
this is Test 1 SomeTest

need to replace TF to ThreeFold
need to replace ThreeFold0 to ThreeFold
need to replace ThreeFold1 to ThreeFold

'

text_out := '

this is TTT SomeTest
this is TTT SomeTest

need to replace ThreeFold to ThreeFold
need to replace ThreeFold to ThreeFold
need to replace ThreeFold to ThreeFold

'

mut ri := regext.regex_instructions_new()
ri.add(['TF:ThreeFold0:ThreeFold1:ThreeFold']) or { panic(err) }
ri.add_item('test_1', 'TTT') or { panic(err) }
ri.add_item('^Stest 1', 'TTT') or { panic(err) } //will be case insensitive search

mut text_out2 := ri.replace(text: text, dedent: true) or { panic(err) }

fn find_sid #

fn find_sid(txt string) []string

find parts of text in form sid:abc till sid:abcde (can be a...z 0...9) . return list of the found elements . to make all e.g. lowercase do e.g. words = words.map(it.to_lower()) after it

fn find_simple_vars #

fn find_simple_vars(txt string) []string

find parts of text which are in form {NAME} . NAME is as follows: . Lowercase letters: a-z . Digits: 0-9 . Underscore: _ . . will return list of the found NAME's

fn regex_instructions_new #

fn regex_instructions_new() ReplaceInstructions

fn regex_rewrite #

fn regex_rewrite(r string) !string

rewrite a filter string to a regex . each char will be checked for in lower case as well as upper case (will match both) . will only look at ascii . '_- ' will be replaced to match one or more spaces . the returned result is a regex string

struct ReplaceArgs #

@[params]
struct ReplaceArgs {
pub mut:
	text   string
	dedent bool
}

struct ReplaceDirArgs #

@[params]
struct ReplaceDirArgs {
pub mut:
	path       string
	extensions []string
	dryrun     bool
}

struct ReplaceInstruction #

struct ReplaceInstruction {
pub:
	regex_str    string
	find_str     string
	replace_with string
pub mut:
	regex regex.RE
}

struct ReplaceInstructions #

struct ReplaceInstructions {
pub mut:
	instructions []ReplaceInstruction
}

fn (ReplaceInstructions) add #

fn (mut ri ReplaceInstructions) add(replacelist []string) !

each element of the list can have more search statements . a search statement can have 3 forms.- regex start with ^R see https://github.com/vlang/v/blob/master/vlib/regex/README.md .

  • case insensitive string find start with ^S (will internally convert to regex).
  • just a string, this is a literal find (case sensitive) .input is ["^Rregex:replacewith",...] . input is ["^Rregex:^Rregex2:replacewith"] . input is ["findstr:findstr:replacewith"] . input is ["findstr:^Rregex2:replacewith"] .

fn (ReplaceInstructions) add_from_text #

fn (mut ri ReplaceInstructions) add_from_text(txt string) !

a text input file where each line has one of the following- regex start with ^R see https://github.com/vlang/v/blob/master/vlib/regex/README.md .

  • case insensitive string find start with ^S (will internally convert to regex).
  • just a string, this is a literal find (case sensitive) .example input ''' ^Rregex:replacewith ^Rregex:^Rregex2:replacewith ^Sfindstr:replacewith findstr:findstr:replacewith findstr:^Rregex2:replacewith ^Sfindstr:^Sfindstr2::^Rregex2:replacewith ''''

fn (ReplaceInstructions) replace #

fn (mut self ReplaceInstructions) replace(args ReplaceArgs) !string

this is the actual function which will take text as input and return the replaced result does the matching line per line . will use dedent function, on text

fn (ReplaceInstructions) replace_in_dir #

fn (mut self ReplaceInstructions) replace_in_dir(args ReplaceDirArgs) !int

if dryrun is true then will not replace but just show