简体   繁体   English

解析正则表达式语法问题matlab /八度

[英]parsing regular expression syntax issue matlab / octave

I can parse the regular expression in matlab / octave below: 我可以在下面的matlab / octave中解析正则表达式:

A = 'Var Name 123.5'
[si ei xt mt] = regexp(A, '(\d)*(\.)?(\d)*$')
number = str2num(mt{1})
number =  123.50

But I get a syntax error below most likly caused due to the ] 但是由于[ ],我收到的语法错误最不常见

A='[angle_deg = 75.01323334803705]'
[si ei xt mt] = regexp(A, '(\d)*(\.)?(\d)*$])

how can I fix this regular expression? 如何解决这个正则表达式?

Your regular expression from the first method is fine assuming that you are looking for a number at the end of the string. 假设您要在字符串末尾查找数字,则第一种方法的正则表达式很好。 Because you have an ending ] character in your new string, your regular expression won't work because your string does not end in a number . 由于新字符串中有一个结尾的]字符,因此正则表达式将不起作用,因为您的字符串不是以数字结尾 As such, simply removing the $ character should work, as you want to search for one number that may or may not be floating point. 因此,只需删除$字符即可,因为您要搜索一个可能是浮点数也可能不是浮点数的数字。 You have three capturing groups in your regex , where the first capture group grabs the integer part of the number, the second capture group optionally grabs a decimal point, and the last capture group grabs the floating point portion of your number. regex有三个捕获组,其中第一个捕获组捕获数字的整数部分,第二个捕获组可选地捕获小数点,最后一个捕获组捕获数字的浮点部分。

You also did not close your string properly in your regex . 您也没有在regex正确关闭字符串。 It needs an ending single quotation. 它需要结尾的单引号。 Therefore: 因此:

A='[angle_deg = 75.01323334803705]';
[si ei xt mt] = regexp(A, '(\d)*(\.)?(\d)*');

Displaying all of the output variables from regexp , this is what I get: 显示regexp所有输出变量,这是我得到的:

>> si

si =

    14

>> ei

ei =

    30

>> xt

xt = 

    [3x2 double]

>> mt

mt = 

    '75.01323334803705'

si denotes the starting index of where the match happened, which is index 14 in your string. si表示匹配发生的起始索引,它是字符串中的索引14。 ei denotes the ending index of where the match happened, which is index 30. xt shows you the starting and ending index that matched each token or capturing group of your regular expression. ei表示匹配发生的结束索引,即索引xt向您显示与正则表达式的每个标记或捕获组匹配的起始索引和结束索引。 To display this, simply do: 要显示此内容,只需执行以下操作:

>> xt{1}

ans =

    14    15
    16    16
    17    30

Therefore, the first capturing group begins at index 14 and ends at index 15, which is the 75 portion of your number. 因此,第一个捕获组从索引14开始,到索引15结束,索引15是数字的75部分。 The second capturing group begins at index 16 and also ends there, which denotes the . 第二个捕获组从索引16开始,也从索引16结束,它表示. character. 字符。 Finally, index 17 to 30 denote the floating point portion of your number, which is 01323334803705 . 最后,索引17到30表示数字的浮点部分,即01323334803705 To finish it all off, mt shows you the extracted string that matched the regular expression, which is the number at the end of this string. 为了完成所有工作, mt向您显示与正则表达式匹配的提取字符串,该字符串是此字符串末尾的数字。 You can certainly convert this string into a number by using str2num . 您当然可以使用str2num将该字符串转换为数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM