正则表达式得到以下模式

本文关键字:模式 正则表达式 | 更新日期: 2023-09-27 18:05:21

你好,我有以下代码行

{
.....(here i can have anything, numbers, string special characters
...."hello"
}

我需要知道为我找到上述模式的Regex模式,其中字符串从{开始,然后是一些文本,然后是关键字如"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)

我想要一个正则表达式,当它在该函数中找到一个关键字时,它会给我一个函数。

有道理吗?

正则表达式得到以下模式

我不知道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);
}