フィルターのクリア

How to parse information between two strings using regular expressions?

16 ビュー (過去 30 日間)
Math
Math 2014 年 12 月 1 日
コメント済み: Math 2014 年 12 月 2 日
Hello,
I am trying to parse some information contained between the two strings "<sample>" and "</sample>" . I am new to regular expressions and would like to know what expression suits my requirement. The strings i mentioned have some operators in them. This is making the job difficult.
Regards, Math
  2 件のコメント
Guillaume
Guillaume 2014 年 12 月 1 日
If you need more help than Thorsten's answer (which pretty much tells you everything that there is to it), then show us your current regular expression.
Math
Math 2014 年 12 月 2 日
編集済み: Math 2014 年 12 月 2 日
"str = '<sample>a,b,c</sample>' "
I want to extract a,b,c from the str above into another string (say extract = a,b,c).
I wrote the following pattern:
"pat = '(<sample>)||(<\/sample>)' "
However, extract = regexp(str,pat,'match') yields " '<sample></sample>' " and not a,b,c.
help regexp says
| Match subexpression before or after the |
How do i get a,b,c to the string variable extract ? Please help.

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 12 月 2 日
編集済み: Andrei Bobrov 2014 年 12 月 2 日
str = '<sample>a,b,c</sample>';
out = regexp(str,'((?<=<sample>).*(?=<\/sample>))','match')
or
t = regexp(str,'<(|\/)sample>','splite')
out = t(~cellfun(@isempty,t))

その他の回答 (2 件)

Thorsten
Thorsten 2014 年 12 月 1 日
編集済み: Thorsten 2014 年 12 月 1 日
help regexp
There it says
Characters that are not special metacharacters are all treated literally in
a match. To match a character that is a special metacharacter, escape that
character with a '\'.
  1 件のコメント
Math
Math 2014 年 12 月 2 日
Please read my latest comment to the question above.

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


Niels
Niels 2014 年 12 月 2 日
Alternatively, you may also consider using regexprep instead of regexp.
>> extract = regexprep(str,pat,'')
extract =
a,b,c
  1 件のコメント
Math
Math 2014 年 12 月 2 日
Thanks Niels. This solves the purpose too.

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

カテゴリ

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