How to compare string with regular expression using SV/UVM constructs? I tried to use uvm_re_match(re, string) function but it did not work. Below code snippet always gives mismatch without uvm_glob_to_re() and always matches with the method uncommented.
module top;
import uvm_pkg::*;
`include "uvm_macros.svh"
string name = "C Trip";
string re = "C T.*";
initial
begin
automatic bit success;
//re = uvm_glob_to_re(re);
success = uvm_re_match(re, name);
if(success)
$display("Match Detected");
else
$display("Match not Detected");
$display("End of Test");
$finish;
end
endmodule
In reply to MayurKubavat:
The value returned by uvm_re_match is an error code from regexec. So 0 is in fact successful.
Dave.
I have asked the same question. I am thinking it would be great if UVM can add one line of explanation on uvm_re_match(), instead of users have to look into the code or C code to figure it out. IMHO.
In reply to dave_59:
Dave.
I have asked the same question. I am thinking it would be great if UVM can add one line of explanation on uvm_re_match(), instead of users have to look into the code or C code to figure it out. IMHO.
Yes. That would be helpful. I also tried doing comparison as below, but its misleading.
// Does not work
if(uvm_re_match(re, name))
$display("Match Detected");
// Works
if(!uvm_re_match(re, name))
$display("Match Detected");
Hi Dave,
thanks for your answers,i have another question regarding regular expressions in systemverilog.
Is it possible to extract a group (%1,%2 …) from the regular expression to another string?
I mean if you have: “abc(de)fg” and you want just “de” using regular expressions in another string how would you do this?. (please assume that the regular expressions are needed because of a complex string)
I know that we have str.substr(startposition, endposition); to extract strings, maybe that can be done somehow with that function using a loop. “uvm_re_match(re, name);” does not provide you the index of the match which would be very helpful.
Looking forward your answer
Another point that may somebody find useful is that we can use $system and file exchange to use LINUX regular expression and other LINUX commands. I know that is not a efficient way to work with strings, but if you need to do it only once at the beginning of your simulation it could be worth it.
For example, here some functions to extract information from LINUX, like the time of simulation, pwd and space left. The extraction of the interesting data is done with a combination of commands in LINUX and systemverilog.
e.g. “df -kh --output=avail .| grep -v Avail”
From that idea, then you could use “sed*/awk”, to work with the strings and regular expression.