From: us on 4 Jul 2010 11:02 "per isakson" <poi.nospam(a)bimDOTkthDOT.se> wrote in message <i0q6nl$3pr$1(a)fred.mathworks.com>... > "lily " <mediocrity111(a)hotmail.com> wrote in message <i0ou2s$p29$1(a)fred.mathworks.com>... > > "per isakson" <poi.nospam(a)bimDOTkthDOT.se> wrote in message <i0nmb3$gen$1(a)fred.mathworks.com>... > > > "lily " <mediocrity111(a)hotmail.com> wrote in message <i0n14d$ecp$1(a)fred.mathworks.com>... > > > > My question is how to use function regexprep to replace a whole string with a part of it. > > > > K>> str='a_b_c.3d.6';regexprep(str,'\w?_(\w+)_\w+?','$1') > > > > ans = > > > > b.3d.6 > > > > What I want is only ‘b’,how to define the pattern?Thanks. > > > > > > >> str='a_b_c.3d.6';regexprep(str,'\w?_(\w+)_.+','$1') > > > ans = > > > b > > > >> > > > > > > I guess \w doesn't match "." > > > > > > / per > > > > 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 why waste precious time on second guessing with a poster who has no clue what he/she wants let alone is able to express him-/herself concisely... to the OP THINK - then POST... us
|
Pages: 1 Prev: equivalent PID in bloc without N filter Next: Help to Enframe audio signal |