View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
Harlan Grove Harlan Grove is offline
external usenet poster
 
Posts: 66
Default extracting numerics from literal strings

"Tushar Mehta" wrote...
Yet another alternative...

If you install the functions at
http://www.tmehta.com/regexp/add_code.htm, and if the string in
question is in column A, you could use the formula

=RegExpSubstitute(A1,"[^\d]*","")

....

While this seems to work, \D it the better/safer/more Perl-compatible way to
express the complement of the character class represented by \d. It also
does more work than it should.

=RegExpSubstitute(A1,"\D+","")

would be optimal in terms of regexp performance. [Tangent: participate in
comp.lang.perl.misc and you'll really learn how to use regular expressions.]

If you have multiple numbers and want only the first set, use
=RegExpSubstitute(A3,"(^[^\d]*)(\d*)(.*$)","$2")


Better to use

=RegExpSubstitute(A3,"^\D*(\d+).*$","$1")

and to pull the last numeral substring,

=RegExpSubstitute(A3,"^.*(\d+)\D*$","$1")

and to pull the n_th numeral substring counting left to right,

=RegExpSubstitute(A3,"^(\D*(\d+)){"&(n-1)&"}\D*(\d+).*$","$3")