From: lily on
"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 &#8216;b&#8217;,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?
From: Rune Allnor on
On 4 Jul, 05:10, "lily " <mediocrity...(a)hotmail.com> wrote:
> "per isakson" <poi.nos...(a)bimDOTkthDOT.se> wrote in message <i0nmb3$ge...(a)fred.mathworks.com>...
> > "lily " <mediocrity...(a)hotmail.com> wrote in message <i0n14d$ec...(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?  

That's a question that is impossible to answer until you explain
what the pattern is: How do you know that it is the 'b' you want
to extract? Because it is located between two underscores? Because
it is the 2nd string fragment in the string? Because it is the
lexicographical 2nd element? Because it is one segment between it
and the dot?

Don't ask *how* to do something until you have a clear idea *what*
it is you want to do.

Rune
 | 
Pages: 1
Prev: Robust standard errors
Next: filter versus smooth