regular expressions resources and regular expression problem

2 ビュー (過去 30 日間)
Patrick Mboma
Patrick Mboma 2012 年 9 月 30 日
Hi all, I don't know anything about regular expressions and I have two questions:
  1. What is the most effective way to learn regular expressions and where to find the best possible tutorials, examples, exercises etc.
  2. I have the following problem. I would like to extract (1) a list variables and (2) a list of matlab functions from a string. The variables have a precise definition: (i) they can start with 'a' 'd' or 'ee'; (2) then an underscore '_'; (3) then an integer 'n'. For example, the variables could be 'a_1', 'd_100', 'ee_3289' and the expression to parse could look like: 'cos(exp(a_2/4*d_1^3-ee_4)+d_85)'. In this example, the list of variables is 'a_2', 'd_1', 'ee_4', 'd_85' and the list of matlab functions is 'cos', 'exp'. My question here is what is the (best) regular expression to extract that information?
Thanks, Pat

採用された回答

Andrei Bobrov
Andrei Bobrov 2012 年 9 月 30 日
編集済み: Andrei Bobrov 2012 年 9 月 30 日
1. Read here about the Regular Expressions
2.
str = 'cos(exp(a_2/4*d_1^3-ee_4)+d_85)';
fun = regexp(str,'\w*(?=\()','match')
var = regexp(str,'(a|d|ee)_\d*','match')
  3 件のコメント
Andrei Bobrov
Andrei Bobrov 2012 年 9 月 30 日
Corrected of var
Daniel Shub
Daniel Shub 2012 年 9 月 30 日
Your regexp foo is so much better than mine. I always have to make things up.

サインインしてコメントする。

その他の回答 (3 件)

Ned Gulley
Ned Gulley 2013 年 5 月 8 日
Another way to get good at regular expressions is to practice on Cody with all the problems that have been tagged "regexp".

Daniel Shub
Daniel Shub 2012 年 9 月 30 日
Assuming a string x,
[a, b] = regexp(x, '(a|d|(ee))(_)([0-9]*)', 'start', 'end')
will find the start and end indices of your "variables." This will allow for a variable a_0 which may or may not be desired. Without a rule for defining functions that handles the numerous edge cases, creating the regexp is difficult. Andrei's answer seems reasonable to me.

Patrick Mboma
Patrick Mboma 2012 年 9 月 30 日
Thank you both. That is one big problem solved. The biggest remains though: how do I become as good as you guys are with regular expressions?
Thanks a lot, Pat.
  2 件のコメント
Walter Roberson
Walter Roberson 2012 年 9 月 30 日
I think there is an O'Reilly book on regular expressions.
The regular expressions used in MATLAB are a mish-mash between capabilities in other languages. They have similarities to Perl, so it might make sense to study the resources for regular expressions in Perl (and there is probably a book or three for that topic alone.)
Patrick Mboma
Patrick Mboma 2012 年 9 月 30 日
Thanks Walter. It is something I will look into.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by