0

こんにちは、次のコード行があります

{
.....(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」のようなキーワードが見つかったときに関数を提供する正規表現が必要です。

理にかなっていますか?

4

2 に答える 2

1

私は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
----------------------------------------------------------------------
于 2013-03-23T12:46:11.073 に答える
0

これはどう:

MatchCollection matches = Regex.Matches(YourText, @"\{[^}]*""hello"".\}", RegexOptions.Multiline);
foreach (Match match in matches)
{
    foreach (Capture capture in match.Captures)
    {
    Console.WriteLine(capture.Value);
}
于 2013-03-23T13:02:05.890 に答える