Extract string with regexp()

5 ビュー (過去 30 日間)
kei hin
kei hin 2021 年 4 月 13 日
コメント済み: kei hin 2021 年 4 月 14 日
I have a variable that stores a string. The string contains letters, numbers, underscores and symbols (there is no specific order, and not just once). Now I want to extract the string through regexp(). The extracted string should start with letters, numbers and underscores and end with letters or numbers, but the suffix needs to exclude '_' or '_in' or '_out' or '_out[]', how can I do that? Thanks.
str_new = regexp(str_old,'[^$^|]\w*_*\w*[^_^_out^_in^_out[]^_in[]]','match')
I want to use '^' to exclude string in '[ ]' , but it is distinguish only one letter or another...
eg:
str_old = '$abc_in'
str_new = abc
str_old = 'def_in_hij_out[]'
str_new = 'def_in_hij'
str_old = 'xyz'
str_new = 'xyz'

採用された回答

Stephen23
Stephen23 2021 年 4 月 13 日
編集済み: Stephen23 2021 年 4 月 13 日
inp = {'$abc_in','def_in_hij_out[]','xyz'}
inp = 1×3 cell array
{'$abc_in'} {'def_in_hij_out[]'} {'xyz'}
out = regexprep(inp,{'_(in|out(\[\])?)?\>','\W+'},'')
out = 1×3 cell array
{'abc'} {'def_in_hij'} {'xyz'}
  1 件のコメント
kei hin
kei hin 2021 年 4 月 14 日
What a nice idea !

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

その他の回答 (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