How to Use Regex in UVM

Sometimes we need to search for a specific string in our test. To do that, we can use regex—everyone loves regex, right?So, how do we do it in UVM?We use a special DPI function (note: this won’t work if UVM_NO_DPI or UVM_REGEX_NO_DPI is defined) called uvm_re_match.The uvm_re_match function takes two arguments:

  • The regular expression
  • The string to search

If there’s a match, uvm_re_match returns 0. If there’s no match, it returns 1.We can also use another DPI function, uvm_glob_to_re, which converts a glob-style expression into a regex.Wondering how to use uvm_glob_to_re? Here’s an example:

Vtool_Tips_Tricks_How_to_use_regex_in_UVM
  • The result is: /^.*my_string.*$/You can copy this into https://regexr.com and experiment with your regex expression!Now, let’s combine these two functions and search for something:

As you can see, I used the escape character \ —this allows us to search within square brackets [ ].And that’s it! Now you know how to use regex in UVM.To wrap things up, here’s a small example on EDA Playground so you can test it and have some fun:https://www.edaplayground.com/x/QB3P