Home > Net >  Convert regex to rust regext. Replace text between nth comma
Convert regex to rust regext. Replace text between nth comma

Time:08-12

The goal is to use a regex to remove text between the nth and the next comma in rust.

For example outside of rust I would use

^((?:.*?,){4})[^,]*,(.*)$

on London, City of Westminster, Greater London, England, SW1A 2DX, United Kingdom

to get a desired result like:

London, City of Westminster, Greater London, England, United Kingdom

I don't have a strong understanding of regex in generall unfortunately. So I would learn more about the mechanic and be able to use it in the program I'm writing to learn rust. Just copy pasting it ala

let string = "London, City of Westminster, Greater London, England, United Kingdom"
let re = Regex::new(r"^((?:.*?,){4})[^,]*,(.*)$").unwrap();
re.replace(string, "");

is not working obviously.

If someone could help me with the conversion and give a hint that helps to understand what needs to happen, it would be marvelous.

CodePudding user response:

The value you want to remove is the fifth comm-delimited value, not the fourth, and you need to replace with two backreferences, $1 and $2 that refer to Group 1 and Group 2 values.

Note it makes it more precise to use a [^,] negated character class rather than a .*? lazy dot in the quantified part since you are running it against a comma-delimited string.

See the Rust demo:

let string = "London, City of Westminster, Greater London, England, SW1A 2DX, United Kingdom";
let re = Regex::new(r"^((?:[^,]*,){4})[^,]*,(.*)").unwrap();
println!("{}", re.replace(string, "$1$2"));
// => London, City of Westminster, Greater London, England, United Kingdom
  • Related