Scan a pattern of characters from a string

2 ビュー (過去 30 日間)
Karthik Brs
Karthik Brs 2015 年 11 月 6 日
コメント済み: Karthik Brs 2015 年 11 月 6 日
Hello everyone, I have a text file from I am required to scan a pattern (string 'SSD' followed by an integer). I am trying to use the 'regexp' command for this operation. For eg. I have used the command 'fgetl' to store a line which contains the pattern I require. Could you help me with this? I want 'ord1' to find the pattern 'SSD1' from 'tline'! Thank you in advance!
tline = ## Step : SSD1
ord1 = regexp(tline,'(?<=SSDd+)','match');

採用された回答

Guillaume
Guillaume 2015 年 11 月 6 日
ord1 = regexp(tline, 'SSD\d+', 'match'); %if you want SSD returned as part of the match
ord1 = regexp(tline, (?<=SSD)\d+', 'match'); %if you just want the number
ord1 = regexp(tline, SSD(\d+), 'tokens', 'once'); just the number, using capture
It is '\d+' to match numbers, the slash is important. '(?<=)' tells the regex engine to look for the pattern preceding a match, but in your regexp you had nothing for the match.
  1 件のコメント
Karthik Brs
Karthik Brs 2015 年 11 月 6 日
Great! Works!

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2015 年 11 月 6 日
Here's an alternate way that is less cryptic than regexp():
tline = '## Step : SSD1'
ssdLocation = strfind(tline, 'SSD') % Find index of SSD
% Extract the end of the string and convert it to a number.
ord1 = str2double(tline(ssdLocation+3:end))
  1 件のコメント
Karthik Brs
Karthik Brs 2015 年 11 月 6 日
Thank You for the reply!

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by