简体   繁体   English

RegEx用于在MATLAB中捕获科学数字

[英]RegEx for capturing scientific numbers in MATLAB

I have the impression I would have a more elegant and compact way of parsing numbers using regexp. 我的印象是,我会使用regexp更优雅,更紧凑地解析数字。 I know how to parse numbers in several formats, 我知道如何解析几种格式的数字,

str_expr = '[\-\+\d\.E]+';
fruit    = regexp(str_text, str_expr, 'match')

And I know how to get a look behind, 而且我知道如何看看背后,

(?<=test)expr

But, what if I want to match several numbers after certain look-behind "test"? 但是,如果我想在某些后面的“测试”后匹配几个数字怎么办? I usually do it in two regexp, one for the look behind, and the second for parsing the numbers. 我通常在两个正则表达式中执行此操作,一个用于后面的查看,第二个用于解析数字。 For example: 例如:

str_text = 'bla, ble,... bli, 1     -0.1243E-02    23.123E+03'

% pre-digest: look behind
str_reg1 = '(?<=bli,).*'
fruit    = regexp(str_text, str_reg1, 'match')

% parse numbers
str_reg2 = '[\-\+\d\.E]+'
fruit    = regexp(fruit{1}, str_reg2, 'match')

If I try to do it in a single step, I only catch the first number 如果我尝试一步完成,我只能抓住第一个数字

% 1st try..
str_reg_try1 = '(?<=bli,)[\-\+\d\.E]+'
fruit        = regexp(fruit{1}, str_reg_try1, 'match')

% 2nd try..
str_reg_try2 = '(?<=bli,)([\-\+\d\.E]+)+'
fruit        = regexp(fruit{1}, str_reg_try1, 'tokens')

How do I solve this problem? 我该如何解决这个问题?

If my guess might be close, then here, we are looking to collect the digits. 如果我的猜测可能接近,那么在这里,我们希望收集数字。 I'm positive that we might want to break the string to two or more parts before we pass it to RegEx engine: 我很肯定我们可能希望在将字符串传递给RegEx引擎之前将其分解为两个或更多部分:

bla, ble,... bli,
1     -0.1243E-02    23.123E+03

Then, in this case, we may not even need regular expression. 然后,在这种情况下,我们甚至可能不需要正则表达式。


Or we might be wanting to just integrate some simple expressions such as: 或者我们可能想要只集成一些简单的表达式,例如:

(.+?bli,)|([0-9-E.\-+]+)

Demo 演示

Also, I think the advice from bobble bubble in the comment is a much better choice ( link ): 另外,我认为评论中bobble bubble的建议是一个更好的选择( 链接 ):

(?:\G(?!^)|bli,)\s+\K[-+\d.E]+

RegEx 正则表达式

If this expression wasn't desired, it can be modified or changed in regex101.com . 如果不需要此表达式,可以在regex101.com中对其进行修改或更改。

RegEx Circuit RegEx电路

jex.im visualizes regular expressions: jex.im可视化正则表达式:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM