A simple regex error

Hi:
I have a simple expression and trying to do a regexp, but it fails. I do not understand why the first regexp fails while the second one succeeds.:
>> str1='peak memory used 124.63 megabytes';
>> tokens=regexp(str1,'peak\s*memory\s*use(\s)*\d')
tokens =
[]
>> tokens=regexp(str1,'peak\s*memory\s*use(\s)*')
tokens =
1

 采纳的回答

use
tokens=regexp(str1,'peak\s*memory\s*use(\s)*d')

2 个评论

Actually, this works. But I always thought that \d is to match a digit. But it looks like it is "d", instead of "/d". I am bit confused about that. Furthermore, if I change the str1 to following, then the regexp still works where it should not.
>> str1='peak memory used d 124.63 megabytes';
>> tokens=regexp(str1,'peak\s*memory\s*use(\s)*d')
tokens =
1
>> str1='peak memory used r 124.63 megabytes';
>> tokens=regexp(str1,'peak\s*memory\s*use(\s)*d')
tokens =
1
What I want is to find a pattern in a textfile which has been read to buffer and I am looking for a number after (with possibly some whitespaces after) the phrase "peak memory used".
Thanks
Hi Bugaboo!
Please try this is code and read about Regular expression in regexp:
str1='peak memory used d 124.63 megabytes';
tokens=regexp(str1,'(?<=peak memory used\s*\w*\s*)\d*\.\d*','match');

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Variables 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by