Skip to content

String and regex replacements #20105

Dec 6, 2023 · 3 comments · 2 replies
Discussion options

You must be logged in to vote

@Brixy I'm supposing that \g is the global flag form PHP regex, then a possible code can be:

import regex
fn sanitize(query string) string {
	mut re_sanitize := regex.regex_opt(r'[^\w.]+') or { panic(0) }
	return re_sanitize.replace(query.replace('ä', 'ae'), '-')
}

fn main() {
	println(sanitize("bäest everum.bat gones_cedum.jpg poster@conen # pero^märcio"))
}
// Output: baeest-everum-gones_cedum-poster-conen-pero-maercio

This code is only an example, it can be optimized better, anyway I'm not sure that regex is the
right solution for this task.

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@Brixy
Comment options

Answer selected by Brixy
Comment options

You must be logged in to vote
1 reply
@Brixy
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants