matlab 提取字符串中白色包围的浮点数

zysjyyx4  于 2022-12-04  发布在  Matlab
关注(0)|答案(3)|浏览(147)

我有类似于这个例子的字符串:

str = '     area                                AMW1  =     93.3 m2 ';

我只想提取浮点数(可能带有符号“-”)93.3。我想提取的浮点数总是被白色包围。
我怎么能那样做呢?
我试过了

s = regexp(str,'\d+\.?\d*','match')

然而,它也匹配12。我找到的其他各种表达式也不起作用。

  • 谢谢-谢谢
3ks5zfa0

3ks5zfa01#

You can use

regexp(str,'-?\d+\.\d+','match')

Or, if you need to also match a + :

regexp(str,'[-+]?\d+\.\d+','match')

If you need to match within whitespace boundaries only:

regexp(str,'(?<!\S)[-+]?\d+\.\d+(?!\S)','match')

If the float value must be in between two whitespace chars:

regexp(str,'(?<=\s)[-+]?\d+\.\d+(?=\s)','match')
  • Details*
  • (?<=\s) - right before the match, there must be a whitespace
  • (?<!\S) - right before the match, there must be start of string, or a whitespace
  • [-+]? - an optional + or -
  • \d+ - one or more digits
  • \. - a dot
  • \d+ - one or more digits
  • (?!\S) - right after, there must be the end of string or a whitespace
  • (?=\s) - right after the match, there must be a whitespace.

And if you need to find both integer or floats, replace \d+\.\d+ with \d+(?:\.\d+)? .

7cwmlq89

7cwmlq892#

试试这个:

re.compile(r'(?<=\s)\d+(\.\d+)?(?=\s)')
5hcedyr0

5hcedyr03#

你在问题中没有提到是否/为什么需要使用regexp。如果字符串组合有一致的模式,你可以只使用sscanftextscan来获取所有非空白元素,而不是regexp

>> str = '     area                                AMW1  =     93.3 m2 '
str =      area                                AMW1  =     93.3 m2

使用sscanf读取可预测格式的字符串:

>> sscanf(str, " %s %s %s %f %s ")
ans =

    97.000
   114.000
   101.000
    97.000
    65.000
    77.000
    87.000
    49.000
    61.000
    93.300
   109.000
    50.000

(for混合类型,字符作为其数字/ascii值存储在数字数组中)
使用*忽略不需要的元素

> sscanf(str, " %*s %*s %*s %f %*s ")
ans = 93.300

也适用于底片:

>> str = '     area                                AMW1  =     -93.3 m2 '
str =      area                                AMW1  =     -93.3 m2

>> sscanf(str, " %*s %*s %*s %f %*s ")
ans = -93.300

类似地,使用textscan,它将把输出存储在单元格数组中:

>> textscan(str, " %s %s %s %f %s ")
ans =
{
  [1,1] =
  {
    [1,1] = area
  }

  [1,2] =
  {
    [1,1] = AMW1
  }

  [1,3] =
  {
    [1,1] = =
  }

  [1,4] = -93.300
  [1,5] =
  {
    [1,1] = m2
  }

}

>> textscan(str, " %*s %*s %*s %f %*s ")
ans =
{
  [1,1] = -93.300
}

>> cell2mat(textscan(s, " %*s %*s %*s %f %*s "))
ans = -93.300

相关问题