MATLAB Answers

regexp help when comparing strings

4 ビュー (過去 30 日間)
Jagath Gamage
Jagath Gamage 2019 年 12 月 9 日
Edited: Stephen Cobeldick 2019 年 12 月 9 日
Hi, I am having trouble with regexp. Why the following expresseion returns empty array ?
regexpi ('a4126643_farfield_(f=2.4)','a4126643_farfield_(f=2.4)')
ans =
[]
Strings which are compared are identical.

  0 件のコメント

サインイン to comment.

採用された回答

Stephen Cobeldick
Stephen Cobeldick 2019 年 12 月 9 日
編集済み: Stephen Cobeldick 2019 年 12 月 9 日
"Strings which are compared are identical"
In general regular expressions are NOT used to compare identical strings (although in some specific circumstances they can do that). If you want to compare identical strings, then use the correct tool: strcmp, strncmp, strcmpi, strncmpi, strfind, contains, ismember, etc..
Regular expressions are not supposed to be identical to the parse string, they are a language that describes pattern matching. If you want to know how to write your own regular expressions, then you will have to read the documentation... and then read it again... and again... and again... (because they are not a trivial language to learn, very powerful certainly, but not trivial):
"Why the following expresseion returns empty array ?"
Because you did not write a regular expression that matches that parse string. Once you read the documentation carefully and escape the active characters, you will get the "expected" output (the start index of the matched string):
>> regexpi ('a4126643_farfield_(f=2.4)','a4126643_farfield_(f=2.4)') % your identical strings
ans = []
>> regexpi ('a4126643_farfield_(f=2.4)','a4126643_farfield_\(f=2\.4\)') % regular expression
ans = 1
You could also escape the active characters using regexptranslate:
A strict "identical" match would also require anchors for the start and end of the text: '^...$'

  0 件のコメント

サインイン to comment.

More Answers (1)

Chuguang Pan
Chuguang Pan 2019 年 12 月 9 日
The function regexpi needs the second argument expression must be a regular expression!
About what is regular expression, you can refer to documentation.

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

タグ

製品


Translated by