What does $1 mean in Perl?

The $number variables contain the parts of the string that matched the capture groups ( … ) in the pattern for your last regex match if the match was successful. For example, take the following string: $text = “the quick brown fox jumps over the lazy dog.”; After the statement $text =~ m/ (b.+?) /; …

Read more

Space or no space

“space or no space” is the same as “zero or one space”, or perhaps “zero or more spaces”, I’m not sure exactly what you want. In the following discussion, I’m going to use <space> to represent a single space, since a single space is hard to see in short code snippets. In the actual regular …

Read more

How do I replace specific characters idiomatically in Rust?

You can replace all occurrences of one string within another with str::replace: let result = str::replace(“Hello World!”, “!”, “?”); // Equivalently: result = “Hello World!”.replace(“!”, “?”); println!(“{}”, result); // => “Hello World?” For more complex cases, you can use regex::Regex::replace_all from regex: use regex::Regex; let re = Regex::new(r”[A-Za-z]”).unwrap(); let result = re.replace_all(“Hello World!”, “x”); println!(“{}”, …

Read more