久久久久久久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 入門)
主站蜘蛛池模板: 亚洲免费观看视频 | 久久男人天堂 | 日本亚洲欧美 | 五月天婷婷综合 | 成人看片网站 | 黄网在线播放 | 911精品国产一区二区在线 | 六十路av | 国产精品美女久久久久av爽 | 日韩福利在线 | 日韩国产综合 | 国产激情久久久 | 亚洲一级片| 国产一区二区欧美 | 日韩在线观看一区 | 超碰在线免费公开 | 午夜久久久久久久 | 国产老头视频 | 国产精自产拍久久久久久蜜 | 亚洲一区二区三区在线视频 | 国产精品久久久久久99 | 天天操天天操天天操 | 欧美精品久久久久久久多人混战 | www.夜夜骑| 亚洲欧美在线视频 | 三级视频网站 | 久久精品视频网站 | 中国黄色一级片 | 国产中文 | 人人草人人草 | 国产视频中文字幕 | 亚洲成人毛片 | 久久久夜色精品 | 蜜桃精品噜噜噜成人av | 亚洲精品中文字幕乱码三区91 | 少妇高潮久久久久久潘金莲 | 欧美一区二区在线视频 | 在线精品一区 | 一区二区三区黄色 | 久久一区视频 | 自拍偷拍中文字幕 |