negative with positive lookbehind regex issue

4 ビュー (過去 30 日間)
Sebastian
Sebastian 2016 年 7 月 17 日
コメント済み: Sebastian 2016 年 7 月 18 日
I'm trying to find the location of open parenthesis, '(', that are preceded by a number, but not when that number is an integer also preceded by the characters 'O' or 'S'.
Example:
str = '12()+F34()+O56()';
should return ind = [3,9], i.e. the open parenthesis following the 12 and 34, but not O56
I tried this:
ind = regexp(str,'(?<=((?<![OS])[0-9]+))[\(]');
but it gives me all of them (ind:[3,9,15]). It does however exclude the cases when there is just a single number after 'O' or 'S' (e.g. str = '12()+F34()+O5()'; -> ind:[3,9])
Does anyone know the proper regular expression for this?
Matlab version: 7.13.0.564 (R2011b)
  1 件のコメント
Stephen23
Stephen23 2016 年 7 月 18 日
編集済み: Stephen23 2016 年 7 月 18 日
@Sebastian: You might like to try using my FEX submission makeregexp:
which lets you interactively develop regular expressions and see regexp's outputs change as you type.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2016 年 7 月 18 日
編集済み: Stephen23 2016 年 7 月 18 日
Try this regular expression: |(?<=\d+)(?<![OS]\d+)\(|
It relies on the fact that lookaround operations do not consume any characters: the first lookaround matches some digits, the second then checks that any digits are not preceded by the letters O or S. Here it is tested:
>> str = '12()+F34()+O56()';
>> regexp(str,'(?<=\d+)(?<![OS]\d+)\(')
ans =
3 9
To help develop this regular expression I used my FEX submission makeregexp:
which lets you interactively develop regular expressions and see regexp's outputs change as you type.
  1 件のコメント
Sebastian
Sebastian 2016 年 7 月 18 日
"lookaround operations do not consume any characters"
Of course, how did I forget. That explains everything. Thanks for the nice regex too.

サインインしてコメントする。

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 7 月 17 日
str = '12()+F34()+O56()';
ii1=regexp(str,'(?<=[OS]\d+)(\()' )
ii2=regexp(str,'(?<=\d+)(\()' )
out=setdiff(ii2,ii1)
  1 件のコメント
Stephen23
Stephen23 2016 年 7 月 18 日
編集済み: Stephen23 2016 年 7 月 18 日
Sebastian's "Answer" moved here:
Thanks Azzi, I will consider that solution. Do you know what is wrong with my initial expression?
Right now I'm resorting to
ind = [regexp(strFunc,'(?<=[^OS0-9][0-9]+)[\(]'),regexp(strFunc,'^[0-9]+[\(]','end')]
which seems to get the job done, but is not exactly the prettiest.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInteractive Model Editing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by