Home > Software design >  How to match regular expression with a dynamic text component
How to match regular expression with a dynamic text component

Time:07-25

I need to make a regular expression match that contains a text component that comes from a variable.

The character vector contained in the variable is long and may contain characters that are operators, quantifiers, metacharacters, etc in Matlab's regular expression engine.

Is there a way to match that text component as is?

Using strfind() would be non-ideal because my desired match comprises of some pattern before the text component, exact match of the text component, and some pattern after. The text can be matched by itself, but matching the components before and after would be inconvenient.

An example (which doesn't necessarily fully capture the inconvenience) would be:

pat_b='%%\s (?<name1>abc\d{6})\s '; % pattern before
txt='asdf(1).qwer=[123:456,789]; zxcv;'; % text from variable
pat_a='\s %%\s (?<name2>\w{6})'; % pattern after

I tried using

f_txt=@()txt;
expression=[pat_b,'(??@f_txt())',pat_a];
regexp(txt_to_search,expression,'names'),

But couldn't get a correct match. Most likely, the result of f_txt() was read without escaping the special characters.

CodePudding user response:

There are two approaches to this situation that use the pattern feature:

  1. Use strfind() to return the number of matches in a string
  2. Use extract() to return the text of the matches in a string

The pattern feature allows you to create complex matching requirements for strfind(), extract(), and other functions. Here is an example using simplified versions of the Question example:

    % a regex at start
    pat_b = 'ab\s[A-Z]';
    % exact text in the middle with random characters
    txt = '-&*\s%%8';
    % a regex at the end
    pat_a = '[a-z]{2,4}';
    
    % create a "pattern" from the three parts
    pat = regexpPattern(pat_b)   txt   regexpPattern(pat_a);
    
    % create test strings for matching
    str1 = "asdfjkl;ab C-&*\s%           
  • Related