From: Samik R. on
Hello,

I am trying to grab numbers which can be decimals (2.3345) or exponents
(10.365E+03 or 110.1132E-10). I am using sed and doing the following for
decimals:
sed '/\([0-9]*.[0-9]*\)/\1/p' ...

which is catching the decimals. How do I modify this to catch the
exponential numbers as well?

Thanks.

-Samik
From: Xicheng Jia on
Samik R. wrote:
> Hello,
>
> I am trying to grab numbers which can be decimals (2.3345) or exponents
> (10.365E+03 or 110.1132E-10). I am using sed and doing the following for
> decimals:
> sed '/\([0-9]*.[0-9]*\)/\1/p' ...
>
> which is catching the decimals. How do I modify this to catch the
> exponential numbers as well?

try:

[0-9]*\.[0-9]*\([Ee][+-]\?[0-9]\+\)\?

Xicheng

From: Eric Moors on
Xicheng Jia wrote:

> Samik R. wrote:
>> Hello,
>>
>> I am trying to grab numbers which can be decimals (2.3345) or exponents
>> (10.365E+03 or 110.1132E-10). I am using sed and doing the following for
>> decimals:
>> sed '/\([0-9]*.[0-9]*\)/\1/p' ...
>>
>> which is catching the decimals. How do I modify this to catch the
>> exponential numbers as well?
>
> try:
>
> [0-9]*\.[0-9]*\([Ee][+-]\?[0-9]\+\)\?
>
> Xicheng

Beware though that you are missing out on valid numbers like 8 or 8E7.4
and are accepting numbers like 8.4.5.6E2

They may fall outside the expected input, but better safe than sorry.

eric
From: Xicheng Jia on
Eric Moors wrote:
> Xicheng Jia wrote:
>
> > Samik R. wrote:
> >> Hello,
> >>
> >> I am trying to grab numbers which can be decimals (2.3345) or exponents
> >> (10.365E+03 or 110.1132E-10). I am using sed and doing the following for
> >> decimals:
> >> sed '/\([0-9]*.[0-9]*\)/\1/p' ...
> >>
> >> which is catching the decimals. How do I modify this to catch the
> >> exponential numbers as well?
> >
> > try:
> >
> > [0-9]*\.[0-9]*\([Ee][+-]\?[0-9]\+\)\?
> >
> > Xicheng
>
> Beware though that you are missing out on valid numbers like 8 or 8E7.4
> and are accepting numbers like 8.4.5.6E2
> They may fall outside the expected input, but better safe than sorry.

Well, I just modified the OP's pattern based on the original
post/examples, if you do want to consider more cases, you can just make
some more adjustments, like from

[0-9]*\.[0-9]*

to

[0-9]*\.\?[0-9]\+

i.e.:

[0-9]*\.\?[0-9]\+\([Ee][+-]\?[0-9]*\.\?[0-9]\+\)\?

which will match 8 and 8E7.4 (you could certainly adjust it to match
8.E7)

If you can fix boundaries, the problem might be simpler. i.e.,
enclosing the pattern with

^ and $

or

\< and \>

or probably

\(^\|[^0-9.]\) and \([^0-9.]\|$\)

or

\(\<\|\.\) and \(\.\|\>\) (may match also '.7.e+.8')

(untested)

This really dependes on the context which the OP didnt mention. For
example, if all these numbers are stand-alone, the pattern could be:

^[0-9]*\(\.[0-9]*\)\?\([Ee][+-]\?[0-9]*\(\.[0-9]*\)\?\)\?$

or probably (no need to be stand-alone numbers but may miss the
leading/trailing 'dot'):

\<[0-9]*\(\.[0-9]*\)\?\([Ee][+-]\?[0-9]*\(\.[0-9]*\)\?\)\?\>

While I knew people here dont like complex stuff like these, I didnt
make my reply this way.. thank you very much for your understanding.

Regards,
Xicheng

From: Samik R. on
On 10/24/2006 9:19 AM, Xicheng Jia wrote:
> Eric Moors wrote:
>> Xicheng Jia wrote:
>>
>>> Samik R. wrote:
>>>> Hello,
>>>>
>>>> I am trying to grab numbers which can be decimals (2.3345) or exponents
>>>> (10.365E+03 or 110.1132E-10). I am using sed and doing the following for
>>>> decimals:
>>>> sed '/\([0-9]*.[0-9]*\)/\1/p' ...
>>>>
>>>> which is catching the decimals. How do I modify this to catch the
>>>> exponential numbers as well?
>>> try:
>>>
>>> [0-9]*\.[0-9]*\([Ee][+-]\?[0-9]\+\)\?
>>>
>>> Xicheng
>> Beware though that you are missing out on valid numbers like 8 or 8E7.4
>> and are accepting numbers like 8.4.5.6E2
>> They may fall outside the expected input, but better safe than sorry.
>
> Well, I just modified the OP's pattern based on the original
> post/examples, if you do want to consider more cases, you can just make
> some more adjustments, like from
>
> [0-9]*\.[0-9]*
>
> to
>
> [0-9]*\.\?[0-9]\+
>
> i.e.:
>
> [0-9]*\.\?[0-9]\+\([Ee][+-]\?[0-9]*\.\?[0-9]\+\)\?
>
> which will match 8 and 8E7.4 (you could certainly adjust it to match
> 8.E7)
>
> If you can fix boundaries, the problem might be simpler. i.e.,
> enclosing the pattern with
>
> ^ and $
>
> or
>
> \< and \>
>
> or probably
>
> \(^\|[^0-9.]\) and \([^0-9.]\|$\)
>
> or
>
> \(\<\|\.\) and \(\.\|\>\) (may match also '.7.e+.8')
>
> (untested)
>
> This really dependes on the context which the OP didnt mention. For
> example, if all these numbers are stand-alone, the pattern could be:
>
> ^[0-9]*\(\.[0-9]*\)\?\([Ee][+-]\?[0-9]*\(\.[0-9]*\)\?\)\?$
>
> or probably (no need to be stand-alone numbers but may miss the
> leading/trailing 'dot'):
>
> \<[0-9]*\(\.[0-9]*\)\?\([Ee][+-]\?[0-9]*\(\.[0-9]*\)\?\)\?\>
>
> While I knew people here dont like complex stuff like these, I didnt
> make my reply this way.. thank you very much for your understanding.
>
> Regards,
> Xicheng
>
Thanks a lot for all the enlightening replies.
-Samik