Why do strfind and regexp may lead to a different result?

7 vues (au cours des 30 derniers jours)
pietro
pietro le 12 Juin 2017
Commenté : pietro le 13 Juin 2017
Hi all,
I noticed that regexp and strfind may lead to different results. Here an example:
regexp('Infinitely Variable Transmission (IVT)','Infinitely Variable Transmission (IVT)')
Why does it occur?
Thanks
  3 commentaires
pietro
pietro le 12 Juin 2017
pardon, I pasted the wrong example.
Stephen23
Stephen23 le 12 Juin 2017
Modifié(e) : Stephen23 le 12 Juin 2017
@pietro: if you are planning on using regular expressions then you really need to understand the different meta-characters, such as parentheses. A good place to learn about regular expressions in MATLAB is to read the MATLAB documentation:

Connectez-vous pour commenter.

Réponse acceptée

Adam
Adam le 12 Juin 2017
Modifié(e) : Adam le 12 Juin 2017
regexp( 'Infinitely Variable Transmission (IVT)', 'Infinitely Variable Transmission \(IVT\)' )
works, but you have to escape the parenthesis as they have special meaning in a regexp expression.
if you look in
doc regexp
in the section headed 'Split Text at Delimiter Using split Keyword', there is an example of this for escaping the ^ symbol.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 12 Juin 2017
In the general case, regexp can return different results if the pattern includes metacharacters.
There is a routine that can be used to "escape" characters so that they are treated literally, but the routine name is not coming to mind at the moment.
  1 commentaire
Walter Roberson
Walter Roberson le 12 Juin 2017
regexptranslate() with the 'escape' option is suitable for this purpose.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by