用正则表达式将科学形式的浮点数与grep匹配

时间:2022-05-21 16:02:57

I am stuck with matching scientific notation of floating point numbers with grep using the regular expression. My problem is as follows:

我坚持使用正则表达式将浮点数的科学符号与grep匹配。我的问题是:

I want to get all the floating point numbers (expressed in scientific notation) in a file, but there are other strings in this file as well. The file content likes like this:

我想在一个文件中获得所有浮点数(用科学符号表示),但是这个文件中还有其他字符串。文件内容如下:

This product is subject to U.S. laws governing export and re-export.
Case run remote-shell.
33080  2.7122e-03  3.7837e-05  4.3489e-05  1.3267e-04  2.7229e-05  4.1196e-05  3.0528e-04  4.7660e-04  5.9506e-06  8.1338e-04  6.6428e-04  1.0230e-04  6.1385e-04  5.3061e-05  4.8918e-04  8.1832e-05  7.5350e-04  6.8355e-04  3.5495e-04  4.3668e-04  1.0076e-04  1.4759e-05  3.3033e-05  3.7662e-05  4.2165e-04  5.1842e-05  3.2158e-04  1.5356e-03  4.2839e-05  2.9283e-04  1.6930e-05  4.9191e-05  3.2135e-05  1.8665e-04  1.6677e-05  3.2952e-04  4.4912e-05  5.6932e-05  1.1145e-03  5.7499e-04  2.1972e-04  3.6925e-05  1.1579e-03  3.9610e-04  3.6176e-04  1.4320e-04  5.9517e-04  2.4946e-04  9.8161e-06  5.3642e-04  5.0760e-03  4.1630e-05  9.5973e-05  5.7817e-05  3.1283e-04  4.4210e-04  9.7502e-06  1.8566e+03  3.0613e-01  0:37:35 1922
33040  3.7547e-03  2.5260e-05  3.0029e-05  9.4277e-05  2.0479e-05  2.4130e-05  1.9597e-04  8.9547e-04  4.2917e-06  5.7030e-04  3.9776e-04  5.8403e-05  3.9431e-04  4.6212e-05  3.2378e-04  4.0916e-05  2.9765e-04  2.7011e-04  2.1954e-04  2.3628e-04  9.6072e-05  1.1480e-05  2.3660e-05  2.4469e-05  2.9498e-04  2.8080e-05  1.9791e-04  1.4410e-03  2.8925e-05  1.8617e-04  1.4366e-05  3.5216e-05  2.9843e-05  1.7923e-04  1.2372e-05  2.0673e-04  3.1176e-05  5.0167e-05  7.0653e-04  3.6454e-04  1.9928e-04  2.2903e-05  8.3425e-04  2.1208e-04  1.7543e-04  9.5440e-05  3.4135e-04  1.7607e-04  7.2080e-06  5.5701e-04  2.9932e-03  3.6117e-05  8.8722e-05  5.1176e-05  2.3192e-04  1.2000e-03  5.9996e-06  1.8570e+03  3.0613e-01  0:38:28 1962

I tried:

我试着:

grep "[0-9]*\.\?[0-9]*[eE][+-][0-9]*" filename

It can indeed filter out all the numbers in scientific notation. But words like re-export and remote-shell are also filtered out. Which is weird to me. Then I removed the \? qualifier:

它确实可以过滤掉科学符号中的所有数字。但是像再出口和远程shell这样的词也被过滤掉了。这对我来说很奇怪。然后我去掉了\?限定符:

grep "[0-9]*\.[0-9]*[eE][+-][0-9]*" filename

This time the words like re-export and remote-shell are not included. But I am really confused as the \? qualifier just represents the preceding item . (dot) is optional, why it makes all everything before it optional? Can anybody explain why this happens and provide a solution to just make the single item before the \? qualifier as optional as later on I also need to filter out numbers like this 2e-3

这一次不包括诸如再出口和远程shell等词。但是我真的很困惑?限定符只表示前面的项。(点)是可选的,为什么所有的东西都是可选的?谁能解释一下为什么会发生这种情况,并提供一种解决方案,使单个项目先于\?和后面的可选限定符一样,我还需要过滤像这个2 -3这样的数字

.

1 个解决方案

#1


2  

The answer as to why it happens is easy: the only obligatory subpatterns in your regex are [eE][+-], so it will match any of e-, e+, E-, E+.

关于发生这种情况的原因,答案很简单:regex中惟一必需的子模式是[eE][+-],因此它将匹配任何e-、e+、e-、e+。

You need to make the number part compulsory. It depends on what kind of number format you need to support, but in most cases you may use + quantifier to match 1 or more occurrences:

你需要使数字部分成为强制性的。这取决于您需要支持哪种数字格式,但是在大多数情况下,您可以使用+量词来匹配1个或多个事件:

"[0-9]*\.\?[0-9]\+[eE][+-][0-9]*"
                ^^

#1


2  

The answer as to why it happens is easy: the only obligatory subpatterns in your regex are [eE][+-], so it will match any of e-, e+, E-, E+.

关于发生这种情况的原因,答案很简单:regex中惟一必需的子模式是[eE][+-],因此它将匹配任何e-、e+、e-、e+。

You need to make the number part compulsory. It depends on what kind of number format you need to support, but in most cases you may use + quantifier to match 1 or more occurrences:

你需要使数字部分成为强制性的。这取决于您需要支持哪种数字格式,但是在大多数情况下,您可以使用+量词来匹配1个或多个事件:

"[0-9]*\.\?[0-9]\+[eE][+-][0-9]*"
                ^^