Table of Contents
[wpbread]
uvm_re_match
Sometimes it is useful to use a regular expression within the testbench, such as filtering based on instance hierarchy. To do this, UVM has a function called uvm_pkg::uvm_re_match(), which is a DPI-C function that makes use of the POSIX function regexec() to perform a string match.
import “DPI-C” function int uvm_re_match ( string re, string str ) ;
Here str is the string and re is the regular expression.
The uvm_re_match function will return zero if there is a match and 1 if the regular expression does NOT match.
uvm_re_match function will not work if code defines UVM_NO_DPI or UVM_REGEX_NO_DPI.
Example Code
module regex;
import uvm_pkg::*;
bit match;
string str = "test_top.env.agent[1].mon";
initial begin
findStr("env", str); //match - returns 0
findStr(".*mon", str); //match - returns 0
findStr("env.*mon", str); //match - returns 0
findStr("driver", str); //no match - returns 1
end
function void findStr(string re, str);
match = uvm_re_match(re, str);
$display(" MATCH=%0d, string: \"%s\", regex: \"%s\"", match, str, re);
endfunction
endmodule
Output:
MATCH=0, string: “test_top.env.agent[1].mon”, regex: “env”
MATCH=0, string: “test_top.env.agent[1].mon”, regex: “.*mon”
MATCH=0, string: “test_top.env.agent[1].mon”, regex: “env.*mon”
MATCH=1, string: “test_top.env.agent[1].mon”, regex: “driver”
Gotcha
How to search brackets [,] ?
Problem statement : we have string “test_top.env.agent[1].mon” and want to search only the monitor prints of agent[1]
we have to write regular expression -> agent\\[1\\].mon
bracket character must be escaped twice(\\),
as [1] will try to match the character found between the brackets and in SystemVerilog ‘\’ also needs to be escaped.
module regex;
import uvm_pkg::*;
bit match;
string str = "test_top.env.agent[1].mon";
initial begin
findStr("agent[1]", str); //no match - returns 1
findStr("agent\[1\]", str); //no match - returns 1
findStr("agent\\[1\\]", str); //match - returns 0
end
function void findStr(string re, str);
match = uvm_re_match(re, str);
$display("MATCH=%0d, string: \"%s\", regex: \"%s\"", match, str, re);
endfunction
endmodule
Output:
MATCH=1, string: “test_top.env.agent[1].mon”, regex: “agent[1]”
MATCH=1, string: “test_top.env.agent[1].mon”, regex: “agent[1]”
MATCH=0, string: “test_top.env.agent[1].mon”, regex: “agent\[1\]”