>* I have to manually remove the 0x, even though it is a very common way of 
>expressing hex numbers

Adding a few lines to NumberParser>>#nextNumber enables it to parse 0x... 
-----------------------------------
((sourceStream peekFor: $r)) ifTrue: ["<base>r<integer>"
        ...
]
ifFalse: [ 
        (sourceStream peekFor: $x) ifTrue: [ "0x<integer>"
                (integerPart isZero and: [ numberOfTrailingZeroInIntegerPart = 
1]) ifFalse: [ 
                        sourceStream skip: -1.
                        ^ self expected: 'one leading 0 before x' 
                ]. 
                ^ self nextUnsignedIntegerBase: 16
        ]        
].
----------------------------------
0x10. "16"
0xFF + 16rFF = (2 * 0xff). "true"

Best regards,
Henrik

-----Opprinnelig melding-----
Fra: Pharo-users [mailto:pharo-users-boun...@lists.pharo.org] På vegne av Peter 
Uhnak
Sendt: 20 January 2017 16:16
Til: pharo-users@lists.pharo.org
Emne: [Pharo-users] Tools for easy subtext extraction from text

Hi,

what are the tools available from easier text extraction?

The input is unstructured text, but I want to extract portion from it.

I am not looking for an engineered approach (writing a parser or something), 
but something that can be done quickly by hand (i.e. interactively).

For example I have string
str = ' Temperature 0           37C (98F) [0x25] (TMPIN0)'
Now I want to extract '0x25' from it and convert it into integer

In Ruby it is dead simple:
str[/\[(.*)\]/,1].hex # "=> 37" , or .to_i(16)

In Pharo I have to break my fingers first:
rx := '.*\[0x(.*)\].*' asRegex.
rx matches: str.
Integer readFrom: (rx subexpression: 2) base: 16 "=>37".

* I have to know that to get subexpression I have to match first to manipulate 
the internal state
* I have to store the matcher to access the subexpression
* I need to explicitly use some global variable Integer as a conversion utility 
to convert hex to dec
* I have to manually remove the 0x, even though it is a very common way of 
expressing hex numbers


So the question is:
do we have a better way to do these things? And as I've mentioned - the use 
case is interactive coding where you often throw the code away when you are 
done; so dead easy to write and use.

Thanks,
Peter



Reply via email to