Prev: Simulink PLL
Next: THD analysis
From: lily on 5 Jul 2010 00:44 > > Thanks per, but the above string is just an example,I mean, if the whole string contents I don't know,maybe it contains '.' ':' '/' '|' or something else, then how to define the general pattern expression string? > > > > Another question: > > InVars=who('-regexp',[Var,'_',Mode,'_\w+']); > > then I get the variables a_b_c.3d.5 and p_a_b_c.3d.5,but what I want is just the first one, the variables beginning with string 'a', why MATLAB gives the second one in the meanwhile? > > IMO (if you intend to use your code for more than the next few days): > 1. It is difficult to make robust constructs with regular expressions. There is always cases you didn't think of. When you intend to match the full string surrounding the expression with "^" and "$" it makes the more robust. > 2. Constructs with regular expressions are difficult (or worse) to understand. You need to write detailed and correct comments to non-trivial constructs. You will need to fix a problem in a week from now. > 3. Writing comments helps getting the construct to work correctly. What is "\w+?" at the end of your expression supposed to mean? BTW: what is the leading "\w?_" intended to match? > 4. When reading the documentation you need to make experiments with your own code. Did you notice that my construct allows the characters "'.' ':' '/' '|'" after "b_"? > 5. When you made the code work sped an extra 20% time to clean up the code - make it simpler (refactor the code). It pays off the next time you need to work with it. > > I don't understand what exactly you want to achieve with "InVars=who(..." > > / per > > >> str='a_b_c|.3d.6';regexprep(str,'\w?_(\w+)_.+','$1') > ans = > b > >> str='a_b_c|3d.6';regexprep(str,'^\w?_(\w+)_.+$','$1') > ans = > b > >> per, thanks your careful explanation. I am sorry I didn't explain my question clearly, giving troubles to everyone trying to help me. 1. the pattern expression string s = regexprep('str', 'expr', 'repstr'), the ''pattern'' means the second input variable 'expr'. regexprep(str,'\w?_(\w+)_.+','$1'),here ''pattern'' is '\w?_(\w+)_.+'; 2. "\w+?" at the end and the leading "\w?_" intended to match? I don't understand function 'regexprep' and regular expressions quite well, so both of them try to match the characters among symbol '_'. 3.I did run the the command str='a_b_c|.3d.6';egexprep(str,'\w?_(\w+)_.+','$1'); Here I didn't express myself clearly again, I intend to match the whole remaining string after the second symbol '_'. And the '^' and '$' is exactly what I want,thanks again. 4. InVars=who('-regexp',[Var,'_',Mode,'_\w+']); % Var:different variables; % Mode:different modes; % '\w+':match other parameters in the string; % InVars: the wanted variables to be drawn I make this code in order to make draw easy, choosing the same kind of variables to be plotted in one figure. Maybe there are other methods to deal with this question quicker, if you are interested, we could discuss about it. Again thanks for all those helped me. And if you have a question about this message, please let me know and I will try my best to to solve it.
|
Pages: 1 Prev: Simulink PLL Next: THD analysis |