Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a compiled Regexp a global variable?

Tags:

rust

I have several regular expressions that are defined at runtime and I would like to make them global variables.

To give you an idea, the following code works:

use regex::Regex; // 1.1.5  fn main() {     let RE = Regex::new(r"hello (\w+)!").unwrap();     let text = "hello bob!\nhello sue!\nhello world!\n";     for cap in RE.captures_iter(text) {         println!("your name is: {}", &cap[1]);     } } 

But I would like it to be something like this:

use regex::Regex; // 1.1.5  static RE: Regex = Regex::new(r"hello (\w+)!").unwrap();  fn main() {     let text = "hello bob!\nhello sue!\nhello world!\n";     for cap in RE.captures_iter(text) {         println!("your name is: {}", &cap[1]);     } } 

However, I get the following error:

error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants  --> src/main.rs:3:20   | 3 | static RE: Regex = Regex::new(r"hello (\w+)!").unwrap();   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

Does this mean that I need nightly Rust in order to make these variables global, or is there another way to do it?

like image 356
vitiral Avatar asked Feb 03 '16 05:02

vitiral


People also ask

What is a global RegExp?

Global regular expression and print (GREP) is a command line text search utility used in Unix. The "grep" command searches files or standard input for lines that match a given regular expression. It then prints the matching lines to the program's standard output.

Which javascript RegExp literal pattern has the global search flag?

Using the global search flag with exec() RegExp.

Can you add variables to regex?

There is the regex constructor which takes a string, so you can build your regex string which includes variables and then pass it to the Regex cosntructor.

What does the global flag do?

The global search flag makes the RegExp search for a pattern throughout the string, creating an array of all occurrences it can find matching the given pattern.


1 Answers

You can use the lazy_static macro like this:

use lazy_static::lazy_static; // 1.3.0 use regex::Regex; // 1.1.5  lazy_static! {     static ref RE: Regex = Regex::new(r"hello (\w+)!").unwrap(); }  fn main() {     let text = "hello bob!\nhello sue!\nhello world!\n";     for cap in RE.captures_iter(text) {         println!("your name is: {}", &cap[1]);     } } 

If you are using the 2015 edition of Rust, you can still use lazy_static via:

#[macro_use] extern crate lazy_static; 
like image 103
squiguy Avatar answered Oct 01 '22 08:10

squiguy