一千萬個為什麽

搜索

關於科學記數法的數字的正則表達式?

我正在加載一個有類似行的.obj文件

vn 8.67548e-017 1 -1.55211e-016

對於頂點法線。如何檢測它們並將它們帶到雙重符號?

最佳答案

一個效果很好的正則表達式是:

-?[\d.]+(?:e-?\d+)?

轉換為數字可以這樣做:科學記數法C ++中的字符串雙重轉換,我想。

正則表達式是

-?      # an optional -
[\d.]+  # a series of digits or dots (see *1)
(?:     # start non capturing group
  e     # "e"
  -?    # an optional -
  \d+   # digits
)?      # end non-capturing group, make optional

**1) This is not 100% correct, technically there can be only one dot, and before it only one (or no) digit. But practically, this should not happen. So 正則表達式是 a good approximation and false positives should be very unlikely. Feel free to make the regex more specific.*

轉載註明原文: 關於科學記數法的數字的正則表達式?