久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

用于檢測 & 分號終止的 C++ 的正則表達式

Regular expression to detect semi-colon terminated C++ for amp; while loops(用于檢測 amp; 分號終止的 C++ 的正則表達式while 循環)
本文介紹了用于檢測 & 分號終止的 C++ 的正則表達式while 循環的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在我的 Python 應用程序中,我需要編寫一個正則表達式來匹配以分號 (;).例如,它應該匹配:

In my Python application, I need to write a regular expression that matches a C++ for or while loop that has been terminated with a semi-colon (;). For example, it should match this:

for (int i = 0; i < 10; i++);

...但不是這個:

for (int i = 0; i < 10; i++)

這乍一看似乎微不足道,直到您意識到左括號和右括號之間的文本可能包含其他括號,例如:

This looks trivial at first glance, until you realise that the text between the opening and closing parenthesis may contain other parenthesis, for example:

for (int i = funcA(); i < funcB(); i++);

我正在使用 python.re 模塊.現在我的正則表達式看起來像這樣(我已經留下了我的評論,所以你可以更容易地理解它):

I'm using the python.re module. Right now my regular expression looks like this (I've left my comments in so you can understand it easier):

# match any line that begins with a "for" or "while" statement:
^s*(for|while)s*
(  # match the initial opening parenthesis
    # Now make a named group 'balanced' which matches a balanced substring.
    (?P<balanced>
        # A balanced substring is either something that is not a parenthesis:
        [^()]
        | # …or a parenthesised string:
        ( # A parenthesised string begins with an opening parenthesis
            (?P=balanced)* # …followed by a sequence of balanced substrings
        ) # …and ends with a closing parenthesis
    )*  # Look for a sequence of balanced substrings
)  # Finally, the outer closing parenthesis.
# must end with a semi-colon to match:
s*;s*

這對于上述所有情況都非常有效,但是一旦您嘗試使 for 循環的第三部分包含一個函數,它就會中斷,如下所示:

This works perfectly for all the above cases, but it breaks as soon as you try and make the third part of the for loop contain a function, like so:

for (int i = 0; i < 10; doSomethingTo(i));

我認為它會中斷,因為只要在左括號和右括號之間放置一些文本,平衡"組就會匹配包含的文本,因此 (?P=balanced) 部分不會不再起作用,因為它不會匹配(因為括號內的文本不同).

I think it breaks because as soon as you put some text between the opening and closing parenthesis, the "balanced" group matches that contained text, and thus the (?P=balanced) part doesn't work any more since it won't match (due to the fact that the text inside the parenthesis is different).

在我的 Python 代碼中,我使用了 VERBOSE 和 MULTILINE 標志,并像這樣創建正則表達式:

In my Python code I'm using the VERBOSE and MULTILINE flags, and creating the regular expression like so:

REGEX_STR = r"""# match any line that begins with a "for" or "while" statement:
^s*(for|while)s*
(  # match the initial opening parenthesis
    # Now make a named group 'balanced' which matches
    # a balanced substring.
    (?P<balanced>
        # A balanced substring is either something that is not a parenthesis:
        [^()]
        | # …or a parenthesised string:
        ( # A parenthesised string begins with an opening parenthesis
            (?P=balanced)* # …followed by a sequence of balanced substrings
        ) # …and ends with a closing parenthesis
    )*  # Look for a sequence of balanced substrings
)  # Finally, the outer closing parenthesis.
# must end with a semi-colon to match:
s*;s*"""

REGEX_OBJ = re.compile(REGEX_STR, re.MULTILINE| re.VERBOSE)

誰能建議改進這個正則表達式?太復雜了,我無法理解.

Can anyone suggest an improvement to this regular expression? It's getting too complicated for me to get my head around.

推薦答案

您可以編寫一個非常簡單的小程序來完成它,而無需使用正則表達式:

You could write a little, very simple routine that does it, without using a regular expression:

  • 設置一個位置計數器pos,使其指向forwhile 之后的左括號之前.
  • 將左括號計數器 openBr 設置為 0.
  • 現在繼續增加pos,讀取相應位置的字符,當看到左括號時增加openBr,看到右括號時減少它.這將在開始時增加一次,對于for ("中的第一個左括號,對中間的一些括號增加和減少一些,并將其設置回0 當您的 for 括號關閉時.
  • 所以,當 openBr 再次為 0 時停止.
  • Set a position counter pos so that is points to just before the opening bracket after your for or while.
  • Set an open brackets counter openBr to 0.
  • Now keep incrementing pos, reading the characters at the respective positions, and increment openBr when you see an opening bracket, and decrement it when you see a closing bracket. That will increment it once at the beginning, for the first opening bracket in "for (", increment and decrement some more for some brackets in between, and set it back to 0 when your for bracket closes.
  • So, stop when openBr is 0 again.

停止位置是 for(...) 的右括號.現在你可以檢查后面是否有分號.

The stopping positon is your closing bracket of for(...). Now you can check if there is a semicolon following or not.

這篇關于用于檢測 &amp; 分號終止的 C++ 的正則表達式while 循環的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

boost_1_60_0 .zip installation in windows(Windows 中的 boost_1_60_0 .zip 安裝)
How do I get console output in C++ with a Windows program?(如何使用 Windows 程序在 C++ 中獲得控制臺輸出?)
How do I calculate the week number given a date?(如何計算給定日期的周數?)
OpenCV with Network Cameras(帶有網絡攝像機的 OpenCV)
Export all symbols when creating a DLL(創建 DLL 時導出所有符號)
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
主站蜘蛛池模板: 精品国产乱码久久久久久丨区2区 | 精品中文字幕一区 | 三级成人在线 | 欧美一区二区三区在线观看 | 国产精品久久久久久久久久久久久久 | 国产一级在线观看 | 亚洲国产视频一区 | 一级看片免费视频囗交动图 | 天堂成人国产精品一区 | 黄色三级免费网站 | 日韩欧美久久精品 | 欧美爱爱视频网站 | 狠狠色综合久久婷婷 | 国产一区二区三区免费观看在线 | 成人国产精品久久久 | 在线午夜| 在线播放亚洲 | 成人免费观看视频 | 91视频亚洲 | 国产午夜精品久久久久免费视高清 | 成人区精品一区二区婷婷 | 国产精品jizz在线观看老狼 | 久久精品亚洲精品国产欧美 | 黄色片网站国产 | 日韩福利| 中国三级黄色录像 | 国产精品日韩欧美一区二区三区 | 成人乱人乱一区二区三区软件 | 亚洲精品欧美 | 天天曰天天干 | 久久精品亚洲精品国产欧美kt∨ | 中文字幕亚洲精品 | 中文字幕一区二区三区精彩视频 | 国产在线成人 | 韩日在线 | 欧美视频1区 | 国产精品成人在线 | 麻豆久久久9性大片 | 亚洲一区二区精品视频 | 亚洲成人一区二区在线 | 欧美一区二区免费视频 |