フィルターのクリア

How to make correct Regex?

1 回表示 (過去 30 日間)
Nicholas Kavouris
Nicholas Kavouris 2024 年 4 月 19 日
コメント済み: Voss 2024 年 4 月 19 日
I have an output log from a system which i am looking to capture data from. The data is found in the printout in the following phrase:
"actualIntensity: Optional(8.24%)". I would like to capture the 8.24 part of this phrase.
I have been using
regexp(text,'actualIntensity: Optional\(([0-9]*\.[0-9]+)\%)','match')
But this returns the entire phrase.
How can i modify this regex to only return 8.24, without all the other text?
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2024 年 4 月 19 日
text is a built-in function. Naming variables (or scripts for that matters) using function names is not a good practice in MATLAB.
Best to change the variable name; a standard variable name in this context is txt.

サインインしてコメントする。

回答 (1 件)

Voss
Voss 2024 年 4 月 19 日
Use 'tokens' instead of 'match'.
  1 件のコメント
Voss
Voss 2024 年 4 月 19 日
txt = "actualIntensity: Optional(8.24%)";
% original but with 'tokens'
regexp(txt,'actualIntensity: Optional\(([0-9]*\.[0-9]+)\%)','tokens')
ans = 1x1 cell array
{["8.24"]}
% original but with 'tokens','once'
regexp(txt,'actualIntensity: Optional\(([0-9]*\.[0-9]+)\%)','tokens','once')
ans = "8.24"
% removing \ before % and combining the [0-9]
regexp(txt,'actualIntensity: Optional\(([0-9\.]+)%)','tokens','once')
ans = "8.24"
% capturing anything between ( and %) in the input string
regexp(txt,'actualIntensity: Optional\((.*)%)','tokens','once')
ans = "8.24"

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by