簡體   English   中英

有沒有辦法在單詞和字符之間拉出所有字符串?

[英]Is there a way to pull all strings between a word and a character?

我正在嘗試使用正則表達式從長字符串中提取所有函數聲明。 現在我正在嘗試使用模式function\\|(.*?)\\|\\}來提取所有字符串,其中第一個單詞是function ,最后一個字符是}后跟一個空行。 我不太熟悉正則表達式,所以我不確定我做錯了什么,但我無法獲得任何匹配。

是否有更好/更簡單的模式可以用於此目的?

我目前的嘗試/實施:

import re

def pull_functions(source_code):
    pattern = "function\|(.*?)\|\}"

    # substring = re.search(pattern, source_code).group(1)
    # print(substring)
    matches = re.findall(
        pattern=r'function\|(.*?)\|\}',
        string=source_code
    )

    print(matches)

    return

source_code可能是的示例:

function foo()
        public
        returns (bool)
{
        ... Function Contents ...
}

/**
 * Some information about this functions...      
 */
function bar(type v1, type v2)
    public
    returns (bool)
{
    ... Function Contents ...
}

以下正則表達式是否符合您的要求?

(^\s*function\s+(.*\s)+?\})

然后捕獲 Group1。 在方法內部的情況下,只有 {....} 它能夠處理它。

https://regex101.com/r/sUzn5Y/1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM