help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: String Index


From: PhilipNienhuis
Subject: Re: String Index
Date: Mon, 27 Jun 2016 10:54:29 -0700 (PDT)

Thomas D. Dean-2 wrote
> On 06/27/2016 09:10 AM, Mike Miller wrote:
>> On Mon, Jun 27, 2016 at 08:43:40 -0700, Thomas D. Dean wrote:
>>> I played with regexp() some, but, could not find a pattern that matched
>>> just
>>> the characters within the parens.
>>>
>>>> str
>>>
>>>> str = "_units_           (10^15 kg)         (km)          (kg/m^3)   "
>>>> [s, e, te, m, t, nm, sp] = regexp(str,"\(...\){3,5}");m
>>>> m
>>> m =
>>> {
>>>    [1,1] = _units_
>>>    [1,2] =    (10^15 kg)
>>>    [1,3] =        (km)
>>>    [1,4] =       (kg/m^3)
>>>
>>> }
>>>
>>> is close, but, still needs some parsing.  Most likely not better than
>>> using
>>> strtok.
>>
>> Sorry, but that regex is not actually doing anything like what you want.
>>
>> First, single vs double quoting is important when using backslashes in
>> strings. Backslashes in double quoted strings are interpreted by the
>> parser. So your quoted string above is the same as '(...){3,5}'.
>>
>> Second, this regex means "find 3 to 5 groups of any 3 characters". And
>> sure enough, if you look at the sizes of each of the strings in m, they
>> are each 15 characters long.
>>
> 
> Half-hour reading "sed & awk", I found something that is closer.
> 
>  > str = "_units_           (10^15 kg)         (km)          (kg/m^3)   "
>  > [s, e, te, m, t, nm, sp] = regexp(str,'\([0-9a-zA-Z][^(]*');m
> m =
> {
>    [1,1] = (10^15 kg)
>    [1,2] = (km)
>    [1,3] = (kg/m^3)
> }

Good on you! 

If you only want "m", the LHS could simply be
[ ~, ~, ~,  m]

Philip




--
View this message in context: 
http://octave.1599824.n4.nabble.com/String-Index-tp4678013p4678024.html
Sent from the Octave - General mailing list archive at Nabble.com.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]