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

 Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 27 Mai 2013

0 votes

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

2 commentaires

Bugaboo
Bugaboo le 27 Mai 2013
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
Andrei Bobrov
Andrei Bobrov le 27 Mai 2013
Modifié(e) : Andrei Bobrov le 27 Mai 2013
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');

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Variables dans Centre d'aide et File Exchange

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by