[英]Regular expression to get following pattern
嗨,我有以下代碼行
{
.....(here i can have anything, numbers, string special characters
...."hello"
}
我需要知道正則表達式模式,該模式可以為我找到上述模式,其中字符串從{開始,然后是一些文本,然后是諸如“ hello”和}之類的關鍵字。
請幫忙
所以我有
function test(a,b,c){
}
function test2(c,a,d){
if(a > 0){
alert('test');
}
我得到了這個表達式function\\s+(\\S+)\\s*\\((.|\\n)*?\\)\\s*{
這給了我function test(a,b,c)
和
function test2(c,a,d)
我想要一個正則表達式,當它在該函數中找到諸如“ test”之類的關鍵字時,該函數可以為我提供功能。
說得通?
我不知道C#,但喜歡(與.
匹配換行符)的正則表達式:
.*\bfunction\b.+?\bhello\b.+?\}
應該做的工作。
說明:
The regular expression:
(?s-imx:.*\bfunction\b.+?\bhello\b.+?\})
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?s-imx: group, but do not capture (with . matching
\n) (case-sensitive) (with ^ and $ matching
normally) (matching whitespace and #
normally):
----------------------------------------------------------------------
.* any character (0 or more times (matching
the most amount possible))
----------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
----------------------------------------------------------------------
function 'function'
----------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
----------------------------------------------------------------------
.+? any character (1 or more times (matching
the least amount possible))
----------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
----------------------------------------------------------------------
hello 'hello'
----------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
----------------------------------------------------------------------
.+? any character (1 or more times (matching
the least amount possible))
----------------------------------------------------------------------
\} '}'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
這個怎么樣:
MatchCollection matches = Regex.Matches(YourText, @"\{[^}]*""hello"".\}", RegexOptions.Multiline);
foreach (Match match in matches)
{
foreach (Capture capture in match.Captures)
{
Console.WriteLine(capture.Value);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.