久久久久久久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 入門)
主站蜘蛛池模板: 欧美一区二区在线观看视频 | 国产成人精品免费 | 成人在线免费观看av | 日韩高清黄色 | 成人免费在线视频 | 日本不卡一区二区三区 | 精品国产免费一区二区三区演员表 | 亚洲视频一区二区三区四区 | 国产一级免费视频 | 黄色精品 | 国产麻豆乱码精品一区二区三区 | 人人色视频 | 欧美黑人一级爽快片淫片高清 | 三级欧美| 国产成人午夜高潮毛片 | 亚洲精品国产综合区久久久久久久 | 日韩欧美福利视频 | 粉嫩国产精品一区二区在线观看 | 亚洲高清一区二区三区 | cao视频 | 亚洲网站在线播放 | 老妇激情毛片免费 | 在线一区二区国产 | 国产男女视频网站 | 黄色播放 | 麻豆av网站 | 亚洲精品中文在线 | 国产精品久久久久久妇女 | 亚洲精品白浆高清久久久久久 | 欧美八区| 国产永久免费 | 一级毛片免费完整视频 | 91国产视频在线观看 | 在线播放中文字幕 | 国产成人综合一区二区三区 | 一区二区三区四区毛片 | 91精品国产高清一区二区三区 | 国产成人免费视频网站高清观看视频 | 小草久久久久久久久爱六 | 啪啪网页 | 日本久草视频 |