How to use the function regexp?

I am working out problems from Cody and I have seen many top solutions are solved using 'regexp' funciton. Can anyone please explain me the functionality of regexp?
I have already read the matlab documents of this fucntion but still I am unable to understand certain things. For example, in problem 838 of 'Check if number exists in vector' where we need to check that number 'a' is present in the vector. Hence, if a = 2 and b = [1 2 3 4] and when use the command y = any( b(:) == a) it gives me logical output as 1. However, when I try the command regexp '' '(/@ y = any( b(:) == a))' it throws me an empty matrix as the result, why is this?
I don't get it where I am going wrong..?
Thanks in advance!

5 Comments

I don't know well about dynamic regexp command. Just one thing, I think it does not accept normal argument. Someone uses "varargin" argument in Cody.
I'm interesting in this mechanism of the dynamic regular expressions commands that serve a functional purpose — (?@cmd). But I think it is cheating to get lower score by using this technic.
In general, I only really use regexp for parsing and searching strings. I suppose it would be possible to use it for numeric arrays as well, but I don't really see the point, as you can just use functions like any, find, or ismember.
With strings, however, regexp is very useful for extracting bit of the string, or finding locations of specific patterns in the strings.
filename = 'results_01102020.txt';
filedate = regexp(filename,'results_(/d+).txt','tokens'); % Returns the date from the filename
Stephen23
Stephen23 on 11 May 2021
Edited: Stephen23 on 11 May 2021
"Can anyone please explain me the functionality of regexp? "
Regular expressions in this context are a special language for indentifying parts of text based on features of that text.
They are commonly used to extract parts of text from larger text pieces, or to replace parts of text with new text.
In Cody regular expressions are used not for their intended purpose, but to run arbitrary commands within a dynamic regular expression. Because those more complex commands are hidden within the regular expression string, Cody only counts the one REGEXP function call and so the user "cheats" to get lower scores.
This, along with many other "features" of Cody, are why Cody is terrible place to learn good MATLAB code practices.
Tricks in Cody tend to only be useful in Cody.
Good code in my view has several characteristics (not in a particular order):
  1. It is concise. Seperate tasks are split into functions that have a clear, single purpose.
  2. It is well-documented, containing a description of the goal, and the syntax description is only missing if it is blatantly obvious from the names of the variables.
  3. Each idea is commented (an idea can span a single line, or a short section like a loop).
  4. The code can be understood even without the documentation and comments: it uses meaningful names for variables and functions, instead of only using short names just to get to short code.
  5. It is fast.
  6. It is easy to extend (or can already handle more cases). An example would be a function processing a 2D image, but which already uses convn instead of conv2, so it is easy to extend to 3D images.
  7. It can run on many releases of Matlab and Octave.
Cody only attempts to score the first one. The rest is either penalized, or ignored:
  • Functions like cellfun and arrayfun should generally be avoided (with the possible exception of the legacy syntax of cellfun), but tend to get a better score on Cody.
  • Using ans as a variable name is (almost?) never clear naming. It is easy to get confused. Using actual variable allows you to be descriptive and explain what is happening.
  • Comments and documentation are completely ignored.
  • Performance is not taken into account at all, except for a handful of challenges where speed is the explicit goal.
  • Maintainability is not a factor in scoring.
Many of these are easy to understand: it is very difficult to come up with a method to objectively measure these.
You can't learn Matlab from Cody. You can learn Cody from Cody.
@Stephen Cobeldick & @Bob Thompson Thanks again for explaining the reg expression, @Rik: You took your valuable time to write about code and functionality of cody, thank you.
@Atsushi Ueno I didn't know this is dynamic regular expression, when I checked MatLab documentation I open the first link about regexp. Thanks, I learnt there is much more for me to check out.
P.S
Of course, Cody is not the platform to learn MatLab, I am using it test my analytical and programming skills.

Sign in to comment.

Answers (0)

Categories

Asked:

on 11 May 2021

Commented:

on 14 May 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!