フィルターのクリア

Localize the third vector position using regexp

2 ビュー (過去 30 日間)
Richard Wood
Richard Wood 2023 年 12 月 23 日
回答済み: Stephen23 2023 年 12 月 23 日
Hello everyone,
I am analyzing some data files in a loop, which look like this:
#------------------------------------------------------
# Atomistic spin configuration file for vampire v5+
#------------------------------------------------------
# Date: Thu Dec 21 02:16:27 2023
#------------------------------------------------------
Time: 1e-10
Field: -0 -0 -3
Temperature: 0
Magnetisation: 0.0276355 0.000325712 -0.999618
#------------------------------------------------------
Number of spin files: 1
spins-00000000.data
In particular, I am interested in the information localized after "Field:", which has the form of a three-dimensional vector (-0,-0,-3). I want to be able to have access to the third component of this vector, "-3". If I use:
field_str=regexp(meta_file_read,'(?<=^Field:\s+)\S+','match','lineanchors');
I am able to obtain the information localized in the first component of the field vector, "-0". Is there an efficient way to access the third component of the field vector using regexp?

採用された回答

Stephen23
Stephen23 2023 年 12 月 23 日
txt = fileread('test.txt')
txt =
'#------------------------------------------------------ # Atomistic spin configuration file for vampire v5+ #------------------------------------------------------ # Date: Thu Dec 21 02:16:27 2023 #------------------------------------------------------ Time: 1e-10 Field: -0 -0 -3 Temperature: 0 Magnetisation: 0.0276355 0.000325712 -0.999618 #------------------------------------------------------ Number of spin files: 1 spins-00000000.data'
rgx = '(?<=Field(\S+\s+){3})\S+';
out = regexp(txt,rgx,'once','match')
out = '-3'

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by