Is there a way to narrow down string searches to specfics integers/characters?

1 回表示 (過去 30 日間)
I have a program that reads files and organizes them into structures. Unfortunately some of the files are not being read correctly because of the search. Here is an example on what string it searches for
Keyword = 'SLGN ';
Indices = strfind(Text, Keyword);
Instead I want to search for
Keyword = 'SLGN X,'
X being any integer or word. What I really want is that comma that is at the end of the phrase. Simply having SLGN with a space after it causes problems for some of the files since I'm trying to parse the lists rather than the paragraphs.

採用された回答

Walter Roberson
Walter Roberson 2016 年 10 月 18 日
You do not speak about how you are doing the search. You can do that kind of matching with regexp()
Keyword = 'SLGN \w+,'
regexp(WhatYouAreSearching, Keyword)
you might want to add 'match' as an option, depending what you want to do with the result of the match.
  7 件のコメント
Sancheet Hoque
Sancheet Hoque 2016 年 10 月 18 日
Oh that's just part of the file that is being read.
""SLGN 741, MADE, T5, FHS7, (FR5673W, FR8345), LAGOR""
""SLGN 741, but prior to expectation it did not work ""
The two phrases above are just part of the file. I posted a portion to show you what I want the program to find. The top phrase is what I want my code to read and the bottom phrase is what I want my code to ignore My code searches for SLGN, so both phrases are read, but I only want lists with commas read.
Walter Roberson
Walter Roberson 2016 年 10 月 18 日
Keyword = 'SLGN\s+(\S+\s*,\s*)+\S+\s*$'
and add the option 'lineanchors'
This requires at least one comma and permits spaces before and after commas and permits the items to include any character except white space because the "(" followed by a word is required to match and so is word followed immediately by ")". So what it does not allow is if there is no comma or if there is a multiple word phrase between commas or a comma is the last non blank on the line.

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

その他の回答 (0 件)

カテゴリ

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